Skip to content

Commit 7ad3fd0

Browse files
committed
Added test to cover PR
1 parent fad8370 commit 7ad3fd0

File tree

8 files changed

+102
-13
lines changed

8 files changed

+102
-13
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace GitVersionExe.Tests
2+
{
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using GitVersion;
6+
using NSubstitute;
7+
using NUnit.Framework;
8+
9+
[TestFixture]
10+
public class AssemblyInfoFileUpdateTests
11+
{
12+
[Test]
13+
public void ShouldStartSearchFromWorkingDirectory()
14+
{
15+
var fileSystem = Substitute.For<IFileSystem>();
16+
const string workingDir = "C:\\Testing";
17+
using (new AssemblyInfoFileUpdate(new Arguments{ UpdateAssemblyInfo = true }, workingDir, new Dictionary<string, string>(), fileSystem))
18+
{
19+
fileSystem.Received().DirectoryGetFiles(Arg.Is(workingDir), Arg.Any<string>(), Arg.Any<SearchOption>());
20+
}
21+
}
22+
}
23+
}

GitVersionExe.Tests/GitVersionExe.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
<Reference Include="LibGit2Sharp">
4040
<HintPath>..\packages\LibGit2Sharp.0.19.0.0\lib\net40\LibGit2Sharp.dll</HintPath>
4141
</Reference>
42+
<Reference Include="NSubstitute">
43+
<HintPath>..\packages\NSubstitute.1.8.0.0\lib\net45\NSubstitute.dll</HintPath>
44+
</Reference>
4245
<Reference Include="nunit.framework">
4346
<HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
4447
</Reference>
@@ -80,6 +83,7 @@
8083
<Compile Include="GitVersionHelper.cs" />
8184
<Compile Include="MsBuildProjectArgTest.cs" />
8285
<Compile Include="PullRequestInTeamCityTest.cs" />
86+
<Compile Include="AssemblyInfoFileUpdateTests.cs" />
8387
</ItemGroup>
8488
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
8589
<PropertyGroup>

GitVersionExe.Tests/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<package id="ApprovalTests" version="3.0.7" targetFramework="net45" />
44
<package id="ApprovalUtilities" version="3.0.7" targetFramework="net45" />
55
<package id="LibGit2Sharp" version="0.19.0.0" targetFramework="net45" />
6+
<package id="NSubstitute" version="1.8.0.0" targetFramework="net45" />
67
<package id="NUnit" version="2.6.3" targetFramework="net45" />
78
<package id="Shouldly" version="2.2.0" targetFramework="net45" />
89
</packages>

GitVersionExe/AssemblyInfoFileUpdate.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,53 @@ class AssemblyInfoFileUpdate : IDisposable
99
List<Action> restoreBackupTasks = new List<Action>();
1010
List<Action> cleanupBackupTasks = new List<Action>();
1111

12-
public AssemblyInfoFileUpdate(Arguments args, string workingDirectory, Dictionary<string, string> variables)
12+
public AssemblyInfoFileUpdate(Arguments args, string workingDirectory, Dictionary<string, string> variables, IFileSystem fileSystem)
1313
{
1414
if (!args.UpdateAssemblyInfo) return;
1515

1616
if (args.Output != OutputType.Json)
1717
Console.WriteLine("Updating assembly info files");
1818

19-
var assemblyInfoFiles = GetAssemblyInfoFiles(workingDirectory, args);
19+
var assemblyInfoFiles = GetAssemblyInfoFiles(workingDirectory, args, fileSystem);
2020

2121
foreach (var assemblyInfoFile in assemblyInfoFiles)
2222
{
2323
var backupAssemblyInfo = assemblyInfoFile + ".bak";
2424
var localAssemblyInfo = assemblyInfoFile;
25-
File.Copy(assemblyInfoFile, backupAssemblyInfo, true);
25+
fileSystem.Copy(assemblyInfoFile, backupAssemblyInfo, true);
2626
restoreBackupTasks.Add(() =>
2727
{
28-
if (File.Exists(localAssemblyInfo))
29-
File.Delete(localAssemblyInfo);
30-
File.Move(backupAssemblyInfo, localAssemblyInfo);
28+
if (fileSystem.Exists(localAssemblyInfo))
29+
fileSystem.Delete(localAssemblyInfo);
30+
fileSystem.Move(backupAssemblyInfo, localAssemblyInfo);
3131
});
32-
cleanupBackupTasks.Add(() => File.Delete(backupAssemblyInfo));
32+
cleanupBackupTasks.Add(() => fileSystem.Delete(backupAssemblyInfo));
3333

3434
var assemblyVersion = string.Format("{0}.{1}.0.0", variables[VariableProvider.Major], variables[VariableProvider.Minor]);
3535
var assemblyInfoVersion = variables[VariableProvider.InformationalVersion];
3636
var assemblyFileVersion = variables[VariableProvider.AssemblySemVer];
37-
var fileContents = File.ReadAllText(assemblyInfoFile)
37+
var fileContents = fileSystem.ReadAllText(assemblyInfoFile)
3838
.RegexReplace(@"AssemblyVersion\(""\d+.\d+.\d+(.\d+|\*)?""\)", string.Format("AssemblyVersion(\"{0}\")", assemblyVersion))
3939
.RegexReplace(@"AssemblyInformationalVersion\(""\d+.\d+.\d+(.\d+|\*)?""\)", string.Format("AssemblyInformationalVersion(\"{0}\")", assemblyInfoVersion))
4040
.RegexReplace(@"AssemblyFileVersion\(""\d+.\d+.\d+(.\d+|\*)?""\)", string.Format("AssemblyFileVersion(\"{0}\")", assemblyFileVersion));
4141

42-
File.WriteAllText(assemblyInfoFile, fileContents);
42+
fileSystem.WriteAllText(assemblyInfoFile, fileContents);
4343
}
4444
}
4545

46-
static IEnumerable<string> GetAssemblyInfoFiles(string workingDirectory, Arguments args)
46+
static IEnumerable<string> GetAssemblyInfoFiles(string workingDirectory, Arguments args, IFileSystem fileSystem)
4747
{
4848
if (args.UpdateAssemblyInfoFileName != null)
4949
{
5050
var fullPath = Path.Combine(workingDirectory, args.UpdateAssemblyInfoFileName);
5151

52-
if (File.Exists(fullPath))
52+
if (fileSystem.Exists(fullPath))
5353
{
5454
return new[] { fullPath };
5555
}
5656
}
5757

58-
return Directory.GetFiles(workingDirectory, "AssemblyInfo.cs", SearchOption.AllDirectories);
58+
return fileSystem.DirectoryGetFiles(workingDirectory, "AssemblyInfo.cs", SearchOption.AllDirectories);
5959
}
6060

6161
public void Dispose()

GitVersionExe/FileSystem.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace GitVersion
2+
{
3+
using System.Collections.Generic;
4+
using System.IO;
5+
6+
class FileSystem : IFileSystem
7+
{
8+
public void Copy(string @from, string to, bool overwrite)
9+
{
10+
File.Copy(from, to, overwrite);
11+
}
12+
13+
public void Move(string @from, string to)
14+
{
15+
File.Move(from, to);
16+
}
17+
18+
public bool Exists(string file)
19+
{
20+
return File.Exists(file);
21+
}
22+
23+
public void Delete(string path)
24+
{
25+
File.Delete(path);
26+
}
27+
28+
public string ReadAllText(string path)
29+
{
30+
return File.ReadAllText(path);
31+
}
32+
33+
public void WriteAllText(string file, string fileContents)
34+
{
35+
File.WriteAllText(file, fileContents);
36+
}
37+
38+
public IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption)
39+
{
40+
return Directory.GetFiles(directory, searchPattern, searchOption);
41+
}
42+
}
43+
}

GitVersionExe/GitVersionExe.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@
5555
<Compile Include="ArgumentParser.cs" />
5656
<Compile Include="Arguments.cs" />
5757
<Compile Include="ExtensionMethods.cs" />
58+
<Compile Include="FileSystem.cs" />
5859
<Compile Include="GitPreparer.cs" />
5960
<Compile Include="HelpWriter.cs" />
61+
<Compile Include="IFileSystem.cs" />
6062
<Compile Include="ProcessHelper.cs" />
6163
<Compile Include="Program.cs" />
6264
<Compile Include="AssemblyInfo.cs" />

GitVersionExe/IFileSystem.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace GitVersion
2+
{
3+
using System.Collections.Generic;
4+
using System.IO;
5+
6+
public interface IFileSystem
7+
{
8+
void Copy(string from, string to, bool overwrite);
9+
void Move(string from, string to);
10+
bool Exists(string file);
11+
void Delete(string path);
12+
string ReadAllText(string path);
13+
void WriteAllText(string file, string fileContents);
14+
IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption);
15+
}
16+
}

GitVersionExe/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static int Run()
114114
}
115115
}
116116

117-
using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, workingDirectory, variables))
117+
using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, workingDirectory, variables, new FileSystem()))
118118
{
119119
var execRun = RunExecCommandIfNeeded(arguments, workingDirectory, variables);
120120
var msbuildRun = RunMsBuildIfNeeded(arguments, workingDirectory, variables);

0 commit comments

Comments
 (0)