Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/CsProj/FileSystem/DotNetFileSystemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,15 @@ public string LoadContent(string filePath)
{
return File.ReadAllText(filePath);
}

/// <summary>
/// Write all text content to the given filepath
/// </summary>
/// <param name="filePath"></param>
/// <param name="data"></param>
public void WriteAllContent(string filePath, string data)
{
File.WriteAllText(filePath, data);
}
}
}
8 changes: 8 additions & 0 deletions src/CsProj/FileSystem/IFileSystemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,13 @@ public interface IFileSystemProvider
/// </summary>
/// <param name="filePath"></param>
string LoadContent(string filePath);

/// <summary>
/// Writes all the content to the given file as a strings
/// </summary>
/// <param name="filePath"></param>
/// <param name="data"></param>
/// <returns></returns>
void WriteAllContent(string filePath, string data);
}
}
12 changes: 9 additions & 3 deletions src/CsProj/ProjectFileVersionPatcher.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Skarp.Version.Cli.CsProj.FileSystem;

namespace Skarp.Version.Cli.CsProj
{
public class ProjectFileVersionPatcher
{
private readonly IFileSystemProvider _fileSystem;
private XDocument _doc;

public ProjectFileVersionPatcher(IFileSystemProvider fileSystem)
{
_fileSystem = fileSystem;
}

public virtual void Load(string xmlDocument)
{
_doc = XDocument.Parse(xmlDocument, LoadOptions.PreserveWhitespace);
Expand Down Expand Up @@ -67,7 +73,7 @@ private void AddMissingElementToCsProj(string elementName, string value)
if (node == null)
{
throw new ArgumentException(
"Given XML does not contain PackageVersion and cannot locate PropertyGroup to add it to - is this a valid csproj?");
$"Given XML does not contain {elementName} and cannot locate existing PropertyGroup to add it to - is this a valid csproj file?");
}
}

Expand All @@ -82,7 +88,7 @@ private void AddMissingElementToCsProj(string elementName, string value)
/// <param name="filePath">The path of the csproj to write to</param>
public virtual void Flush(string filePath)
{
File.WriteAllText(filePath, _doc.ToString());
_fileSystem.WriteAllContent(filePath, ToXmlString());
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,14 @@ string preReleasePrefix

private static void SetUpDependencies()
{
var dotNetFileSystemProvider = new DotNetFileSystemProvider();
_cli = new VersionCli(
new GitVcs(),
new ProjectFileDetector(
new DotNetFileSystemProvider()
dotNetFileSystemProvider
),
new ProjectFileParser(),
new ProjectFileVersionPatcher(),
new ProjectFileVersionPatcher(dotNetFileSystemProvider),
new SemVerBumper()
);
}
Expand Down
3 changes: 3 additions & 0 deletions src/VersionBump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Skarp.Version.Cli
/// </summary>
public enum VersionBump
{
// Not supplied or parsing error or something - we can't bump `unknown`
Unknown,

Major,

Minor,
Expand Down
11 changes: 1 addition & 10 deletions src/Versioning/SemVer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,7 @@ public string ToSemVerVersionString()

return sb.ToString();
}

/// <summary>
/// Returns a simple version string including only major.minor.patch
/// </summary>
/// <returns></returns>
public string ToSimpleVersionString()
{
return $"{Major}.{Minor}.{Patch}";
}


/// <summary>
/// Create a new instance of a SemVer based off the version string
/// </summary>
Expand Down
24 changes: 12 additions & 12 deletions src/Versioning/SemVerBumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public SemVer Bump(
}
default:
{
throw new ArgumentOutOfRangeException($"VersionBump : {bump} not supported");
throw new ArgumentOutOfRangeException(nameof(bump), $"VersionBump : {bump} not supported");
}
}

Expand Down Expand Up @@ -160,15 +160,15 @@ private void HandlePreMinorBump(SemVer newVersion, string preReleasePrefix)

private void HandleMinorBump(SemVer newVersion)
{
if (!newVersion.IsPreRelease)
if (newVersion.IsPreRelease)
{
newVersion.Minor += 1;
newVersion.Patch = 0;
newVersion.PreRelease = string.Empty;
newVersion.BuildMeta = string.Empty;
}
else
{
newVersion.PreRelease = string.Empty;
newVersion.BuildMeta = string.Empty;
newVersion.Minor += 1;
newVersion.Patch = 0;
}
}

Expand All @@ -187,16 +187,16 @@ private void HandlePreMajorBump(SemVer newVersion, string preReleasePrefix)

private void HandleMajorBump(SemVer newVersion)
{
if (!newVersion.IsPreRelease)
if (newVersion.IsPreRelease)
{
newVersion.Major += 1;
newVersion.Minor = 0;
newVersion.Patch = 0;
newVersion.PreRelease = string.Empty;
newVersion.BuildMeta = string.Empty;
}
else
{
newVersion.PreRelease = string.Empty;
newVersion.BuildMeta = string.Empty;
newVersion.Major += 1;
newVersion.Minor = 0;
newVersion.Patch = 0;
}
}
}
Expand Down
62 changes: 62 additions & 0 deletions test/CsProj/FileSystem/DotNetFileSystemProviderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.IO;
using System.Linq;
using Skarp.Version.Cli.CsProj.FileSystem;
using Xunit;

namespace Skarp.Version.Cli.Test.CsProj.FileSystem
{
public class DotNetFileSystemProviderTests
{
private readonly DotNetFileSystemProvider _provider;

public DotNetFileSystemProviderTests()
{
_provider = new DotNetFileSystemProvider();
}

[Fact]
public void List_works()
{
// List files in the current directory running from (the build output folder)
var files = _provider.List("./").ToList();

Assert.NotEmpty(files);
Assert.Contains("./dotnet-version.dll", files);
}

[Theory]
[InlineData("./dotnet-version.dll", false)]
[InlineData("../../../dotnet-version-test.csproj", true)]
public void IsCsProjectFile_works(string path, bool isCsProj)
{
Assert.Equal(_provider.IsCsProjectFile(path), isCsProj);
}

[Fact]
public void Cwd_works()
{
var cwd = _provider.Cwd();
Assert.Contains($"netcoreapp", cwd);
}

[Fact]
public void LoadAllContent_works()
{
var content = _provider.LoadContent("../../../dotnet-version-test.csproj");
Assert.Contains("<ProjectGuid>fb420acf-9e12-42b6-b724-1eee9cbf251e</ProjectGuid>", content);
}

[Fact]
public void WriteAllContent_works()
{
var path = "./test-file.txt";
var content = "this is content";
_provider.WriteAllContent(path, content);

var loadedContent = File.ReadAllText(path);

Assert.Equal(content, loadedContent);
}
}
}
109 changes: 80 additions & 29 deletions test/CsProj/ProjectFileVersionPatcherTest.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
using System;
using FakeItEasy;
using Skarp.Version.Cli.CsProj;
using Skarp.Version.Cli.CsProj.FileSystem;
using Xunit;

namespace Skarp.Version.Cli.Test.CsProj
{
public class ProjectFileVersionPatcherTest
{
private static string _projectXml =
"<Project Sdk=\"Microsoft.NET.Sdk\">" +
"<PropertyGroup>" +
"<TargetFramework>netstandard1.6</TargetFramework>" +
"<RootNamespace>Unit.For.The.Win</RootNamespace>" +
"<PackageId>Unit.Testing.Library</PackageId>" +
"<Version>1.0.0</Version>" +
"<PackageVersion>1.0.0</PackageVersion>" +
"</PropertyGroup>" +
"</Project>";
"<Project Sdk=\"Microsoft.NET.Sdk\">" +
"<PropertyGroup>" +
"<TargetFramework>netstandard1.6</TargetFramework>" +
"<RootNamespace>Unit.For.The.Win</RootNamespace>" +
"<PackageId>Unit.Testing.Library</PackageId>" +
"<Version>1.0.0</Version>" +
"<PackageVersion>1.0.0</PackageVersion>" +
"</PropertyGroup>" +
"</Project>";

private readonly ProjectFileVersionPatcher _patcher;
private readonly IFileSystemProvider _fileSystem;

public ProjectFileVersionPatcherTest()
{
_patcher = new ProjectFileVersionPatcher();
_fileSystem = A.Fake<IFileSystemProvider>();
_patcher = new ProjectFileVersionPatcher(_fileSystem);
}

[Fact]
Expand All @@ -31,6 +35,7 @@ public void Throws_when_load_not_called()

Assert.IsAssignableFrom<InvalidOperationException>(ex);
}

[Fact]
public void CanPatchVersionOnWellFormedXml()
{
Expand All @@ -41,40 +46,86 @@ public void CanPatchVersionOnWellFormedXml()
Assert.NotEqual(_projectXml, newXml);
Assert.Contains("<Version>1.1.0-0</Version>", newXml);
}

[Fact]
public void CanPatchWhenVersionIsMissing()
{
var xml =
"<Project Sdk=\"Microsoft.NET.Sdk\">" +
"<PropertyGroup>" +
"<TargetFramework>netstandard1.6</TargetFramework>" +
"<RootNamespace>Unit.For.The.Win</RootNamespace>" +
"<PackageId>Unit.Testing.Library</PackageId>" +
"</PropertyGroup>" +
"</Project>";
var xml =
"<Project Sdk=\"Microsoft.NET.Sdk\">" +
"<PropertyGroup>" +
"<TargetFramework>netstandard1.6</TargetFramework>" +
"<RootNamespace>Unit.For.The.Win</RootNamespace>" +
"<PackageId>Unit.Testing.Library</PackageId>" +
"</PropertyGroup>" +
"</Project>";

_patcher.Load(xml);
_patcher.PatchVersionField("1.0.0", "2.0.0");
var newXml = _patcher.ToXmlString();
Assert.Contains("<Version>2.0.0</Version>", newXml);
}
}

[Fact]
public void PreservesWhiteSpaceWhilePatching()
{
var xml =
"<Project Sdk=\"Microsoft.NET.Sdk\">" +
"<PropertyGroup>" +
"<Version>1.0.0</Version>" +
"</PropertyGroup>" +
$"{Environment.NewLine}{Environment.NewLine}{Environment.NewLine}{Environment.NewLine}" +
"</Project>";
var xml =
"<Project Sdk=\"Microsoft.NET.Sdk\">" +
"<PropertyGroup>" +
"<Version>1.0.0</Version>" +
"</PropertyGroup>" +
$"{Environment.NewLine}{Environment.NewLine}{Environment.NewLine}{Environment.NewLine}" +
"</Project>";

_patcher.Load(xml);
_patcher.PatchVersionField("1.0.0", "2.0.0");
var newXml = _patcher.ToXmlString();
Assert.Contains($"{Environment.NewLine}{Environment.NewLine}{Environment.NewLine}{Environment.NewLine}", newXml);
Assert.Contains($"{Environment.NewLine}{Environment.NewLine}{Environment.NewLine}{Environment.NewLine}",
newXml);
}

[Fact]
public void HandlesMissingVersionWhenTargetFrameworksField()
{
var xml =
"<Project Sdk=\"Microsoft.NET.Sdk\">" +
"<PropertyGroup>" +
"<TargetFrameworks>netstandard1.6;dotnet462</TargetFrameworks>" +
"</PropertyGroup>" +
"</Project>";

_patcher.Load(xml);
_patcher.PatchVersionField("1.0.0", "2.0.0");
var newXml = _patcher.ToXmlString();
Assert.Contains("<Version>2.0.0</Version>", newXml);
}

[Fact]
public void BailsWhenUnableToLocatePropertyGroup()
{
var xml =
"<Project Sdk=\"Microsoft.NET.Sdk\">" +
"</Project>";

_patcher.Load(xml);
var ex = Record.Exception(() => _patcher.PatchVersionField("1.0.0", "2.0.0"));

var aex = Assert.IsAssignableFrom<ArgumentException>(ex);

Assert.Equal(
"Given XML does not contain Version and cannot locate existing PropertyGroup to add it to - is this a valid csproj file?",
aex.Message
);
}

[Fact]
public void Flush_calls_filesystem()
{
_patcher.Load(_projectXml);

var thePath = "/some/path.txt";
_patcher.Flush(thePath);

A.CallTo(() => _fileSystem.WriteAllContent(thePath, A<string>._)).MustHaveHappenedOnceExactly();
}
}
}
Loading