Skip to content

Cache version information to disk #711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
659cff4
Enable disk caching of version info.
asbjornu Aug 25, 2015
c20abdb
Do scoped `IndentLog` instead of `WriteInfo` before and after.
asbjornu Oct 22, 2015
fbc0d87
Write != Read.
asbjornu Oct 23, 2015
242a074
Add .yml extension to the cached version file
asbjornu Oct 23, 2015
b54d7f4
Improved logging scopes and code readability
asbjornu Oct 23, 2015
c2766ec
Clarify that we're loading version from disk cache if it can't be fou…
asbjornu Nov 10, 2015
fe29d11
We don't need to check if directories exist before doing CreateDirect…
asbjornu Nov 10, 2015
5259594
R# cleanup
asbjornu Nov 10, 2015
407a9e3
Moved TreeWalkForDotGitDir() from GitDirFinder to IFileSystem to make…
asbjornu Nov 10, 2015
5e4532e
Moved RepositoryLoader.GetRepo() to IFileSystem.GetRepository() to ma…
asbjornu Nov 10, 2015
c16acbc
Moved GetLastDirectoryWrite() from DirectoryDateFinder to IFileSystem…
asbjornu Nov 10, 2015
a37d211
IRepository is easier to mock than Repository
asbjornu Nov 10, 2015
41bb6eb
Added NSubstitute NuGet package reference to make mocking a non-tedio…
asbjornu Nov 10, 2015
6a0fa0f
Log if the version cache file can't be found
asbjornu Nov 10, 2015
b1e41fa
Added tests for VersionBranchFinder.GetVersion() and its use of the v…
asbjornu Nov 10, 2015
a5d425b
R# cleanup
asbjornu Nov 13, 2015
82a1346
Use string.Equals() with explicit StringComparison
asbjornu Nov 13, 2015
56b9b2d
Use the AvailableVariables property instead of doing GetProperties() …
asbjornu Nov 13, 2015
6638e89
Don't crash if cleanup fails
asbjornu Nov 13, 2015
0114126
Add [ReflectionIgnore] attribute to properties in VersionVariables so…
asbjornu Nov 13, 2015
216c044
Added `VersionVariables.FromFile()` method and corresponding `FileNam…
asbjornu Nov 13, 2015
ed5edd6
Make the `VersionAndBranchFinder.VersionCacheVersions` dictionary int…
asbjornu Nov 13, 2015
5000e3c
Rewrote the VersionAndBranchFinder tests so they do actual file syste…
asbjornu Nov 13, 2015
1c2c273
Made the VersionAndBranchFinder tests a bit more DRY
asbjornu Nov 13, 2015
ba89853
Deleted `IFileSystem.TreeWalkForDotGitDir()` and replaced its usage w…
asbjornu Nov 16, 2015
7b5e3c5
Removed license
asbjornu Nov 16, 2015
817729d
Removed mocking of IRepository since it's not being used by any of th…
asbjornu Nov 16, 2015
c6943e6
R# cleanup
asbjornu Nov 16, 2015
f7f95c2
Refactored `VersionAndBranchFinder` to a non-static file.
asbjornu Nov 16, 2015
5dd4d3f
Avoid `NullReferenceException` in `Logger` by null-guarding the logge…
asbjornu Nov 16, 2015
207cc67
Deleted `VersionAndBranchFinder`, moved its caching logic into `Execu…
asbjornu Nov 16, 2015
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
115 changes: 115 additions & 0 deletions src/GitVersionCore.Tests/ExecuteCoreTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.Collections.Concurrent;
using System.Text;

using GitVersion;
using GitVersion.Helpers;

using NUnit.Framework;

using Shouldly;

[TestFixture]
public class ExecuteCoreTests
{
IFileSystem fileSystem;


[SetUp]
public void SetUp()
{
this.fileSystem = new FileSystem();
}


[Test]
public void CacheFileExistsOnDisk()
{
const string versionCacheFileContent = @"
Major: 4
Minor: 10
Patch: 3
PreReleaseTag: test.19
PreReleaseTagWithDash: -test.19
BuildMetaData:
BuildMetaDataPadded:
FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
MajorMinorPatch: 4.10.3
SemVer: 4.10.3-test.19
LegacySemVer: 4.10.3-test19
LegacySemVerPadded: 4.10.3-test0019
AssemblySemVer: 4.10.3.0
FullSemVer: 4.10.3-test.19
InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
BranchName: feature/test
Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
NuGetVersionV2: 4.10.3-test0019
NuGetVersion: 4.10.3-test0019
CommitsSinceVersionSource: 19
CommitsSinceVersionSourcePadded: 0019
CommitDate: 2015-11-10
";

var versionAndBranchFinder = new ExecuteCore(this.fileSystem);

var info = RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
{
this.fileSystem.WriteAllText(vv.FileName, versionCacheFileContent);
vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
vv.AssemblySemVer.ShouldBe("4.10.3.0");
});

info.ShouldContain("Deserializing version variables from cache file", () => info);
}


[Test]
public void CacheFileExistsInMemory()
{
var cache = new ConcurrentDictionary<string, VersionVariables>();
var versionAndBranchFinder = new ExecuteCore(this.fileSystem, cache.GetOrAdd);

var info = RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
{
vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
vv.AssemblySemVer.ShouldBe("0.1.0.0");
});

info.ShouldContain("yml not found", () => info);
info.ShouldNotContain("Deserializing version variables from cache file", () => info);
}


[Test]
public void CacheFileIsMissing()
{
var info = RepositoryScope();
info.ShouldContain("yml not found", () => info);
}


string RepositoryScope(ExecuteCore executeCore = null, Action<EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
{
var infoBuilder = new StringBuilder();
Action<string> infoLogger = s => { infoBuilder.AppendLine(s); };
executeCore = executeCore ?? new ExecuteCore(this.fileSystem);

Logger.SetLoggers(infoLogger, s => { }, s => { });

using (var fixture = new EmptyRepositoryFixture(new Config()))
{
fixture.Repository.MakeACommit();
var vv = executeCore.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);

vv.AssemblySemVer.ShouldBe("0.1.0.0");
vv.FileName.ShouldNotBeNullOrEmpty();

if (fixtureAction != null)
{
fixtureAction(fixture, vv);
}
}

return infoBuilder.ToString();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.IO;
using GitVersion;
using GitVersion.Helpers;

using LibGit2Sharp;
using NUnit.Framework;

[TestFixture]
public class GitDirFinderTests
public class FileSystemTests
{
string workDirectory;
string gitDirectory;
Expand All @@ -15,8 +17,7 @@ public void CreateTemporaryRepository()
{
workDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

gitDirectory = Repository.Init(workDirectory)
.TrimEnd(new[] { Path.DirectorySeparatorChar });
gitDirectory = Repository.Init(workDirectory);

Assert.NotNull(gitDirectory);
}
Expand All @@ -30,26 +31,26 @@ public void Cleanup()
[Test]
public void From_WorkingDirectory()
{
Assert.AreEqual(gitDirectory, GitDirFinder.TreeWalkForDotGitDir(workDirectory));
Assert.AreEqual(gitDirectory, Repository.Discover(workDirectory));
}

[Test]
public void From_WorkingDirectory_Parent()
{
var parentDirectory = Directory.GetParent(workDirectory).FullName;
Assert.Null(GitDirFinder.TreeWalkForDotGitDir(parentDirectory));
Assert.Null(Repository.Discover(parentDirectory));
}

[Test]
public void From_GitDirectory()
{
Assert.AreEqual(gitDirectory, GitDirFinder.TreeWalkForDotGitDir(gitDirectory));
Assert.AreEqual(gitDirectory, Repository.Discover(gitDirectory));
}

[Test]
public void From_RefsDirectory()
{
var refsDirectory = Path.Combine(gitDirectory, "refs");
Assert.AreEqual(gitDirectory, GitDirFinder.TreeWalkForDotGitDir(refsDirectory));
Assert.AreEqual(gitDirectory, Repository.Discover(refsDirectory));
}
}
2 changes: 2 additions & 0 deletions src/GitVersionCore.Tests/Fixtures/RemoteRepositoryFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using GitVersion;
using GitVersion.Helpers;

using LibGit2Sharp;

public class RemoteRepositoryFixture : RepositoryFixtureBase
Expand Down
2 changes: 2 additions & 0 deletions src/GitVersionCore.Tests/GitPreparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.IO;
using System.Linq;
using GitVersion;
using GitVersion.Helpers;

using LibGit2Sharp;
using NUnit.Framework;
using Shouldly;
Expand Down
7 changes: 6 additions & 1 deletion src/GitVersionCore.Tests/GitVersionCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
<HintPath>..\packages\Mono.Cecil.0.9.6.1\lib\net45\Mono.Cecil.Rocks.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NSubstitute, Version=1.9.2.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca, processorArchitecture=MSIL">
<HintPath>..\packages\NSubstitute.1.9.2.0\lib\net45\NSubstitute.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.core, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -124,7 +128,7 @@
<Compile Include="ConfigProviderTests.cs" />
<Compile Include="Fixtures\LocalRepositoryFixture.cs" />
<Compile Include="Fixtures\RemoteRepositoryFixture.cs" />
<Compile Include="GitDirFinderTests.cs" />
<Compile Include="FileSystemTests.cs" />
<Compile Include="Fixtures\BaseGitFlowRepositoryFixture.cs" />
<Compile Include="GitHelperTests.cs" />
<Compile Include="GitPreparerTests.cs" />
Expand Down Expand Up @@ -174,6 +178,7 @@
<Compile Include="TestFileSystem.cs" />
<Compile Include="TestStream.cs" />
<Compile Include="VariableProviderTests.cs" />
<Compile Include="ExecuteCoreTests.cs" />
<Compile Include="VersionCalculation\BaseVersionCalculatorTests.cs" />
<Compile Include="VersionCalculation\NextVersionCalculatorTests.cs" />
<Compile Include="VersionCalculation\Strategies\ConfigNextVersionBaseVersionStrategyTests.cs" />
Expand Down
55 changes: 50 additions & 5 deletions src/GitVersionCore.Tests/TestFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,97 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using GitVersion.Helpers;

using LibGit2Sharp;

public class TestFileSystem : IFileSystem
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GeertvanHorrik fyi. Maybe just make the file system stuff internal in Core. The needed abstractions will be different in each app

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I hoped the abstraction would be useful, but I ended up not using it. Mocking with static classes and methods is nigh impossible.

{
Dictionary<string, string> fileSystem = new Dictionary<string, string>();


public void Copy(string @from, string to, bool overwrite)
{
throw new NotImplementedException();
}


public void Move(string @from, string to)
{
throw new NotImplementedException();
}


public bool Exists(string file)
{
return fileSystem.ContainsKey(file);
return this.fileSystem.ContainsKey(file);
}


public void Delete(string path)
{
throw new NotImplementedException();
}


public string ReadAllText(string path)
{
return fileSystem[path];
return this.fileSystem[path];
}


public void WriteAllText(string file, string fileContents)
{
if (fileSystem.ContainsKey(file))
fileSystem[file] = fileContents;
if (this.fileSystem.ContainsKey(file))
{
this.fileSystem[file] = fileContents;
}
else
fileSystem.Add(file, fileContents);
{
this.fileSystem.Add(file, fileContents);
}
}


public IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption)
{
throw new NotImplementedException();
}


public Stream OpenWrite(string path)
{
return new TestStream(path, this);
}


public Stream OpenRead(string path)
{
if (this.fileSystem.ContainsKey(path))
{
var content = this.fileSystem[path];
return new MemoryStream(Encoding.UTF8.GetBytes(content));
}

throw new FileNotFoundException("File not found.", path);
}


public void CreateDirectory(string path)
{
}


public long GetLastDirectoryWrite(string path)
{
return 1;
}


public IRepository GetRepository(string gitDirectory)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the Fixture stuff? Mocking out libgit2sharp stuff is annoying in the long run I think

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JakeGinnivan Yea, I ended up using the fixture stuff, which is why I ask what is causing the following exception:

GitVersion.WarningException : 0 remote(s) have been detected. When being run on a build server, the Git repository is expected to bear one (and no more than one) remote.

Why are none of the other tests using the fixture having this problem? I see no mention of any remotes.

{
throw new NotImplementedException();
}
}
1 change: 1 addition & 0 deletions src/GitVersionCore.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<package id="LibGit2Sharp" version="0.21.0.176" targetFramework="net45" />
<package id="ModuleInit.Fody" version="1.5.8.0" targetFramework="net45" developmentDependency="true" />
<package id="Mono.Cecil" version="0.9.6.1" targetFramework="net45" />
<package id="NSubstitute" version="1.9.2.0" targetFramework="net45" />
<package id="NUnit" version="2.6.4" targetFramework="net45" />
<package id="NUnitTestAdapter" version="2.0.0" targetFramework="net45" />
<package id="Shouldly" version="2.5.0" targetFramework="net45" />
Expand Down
Loading