diff --git a/GitVersionExe.Tests/AssemblyInfoFileUpdateTests.cs b/GitVersionExe.Tests/AssemblyInfoFileUpdateTests.cs new file mode 100644 index 0000000000..4e838cac68 --- /dev/null +++ b/GitVersionExe.Tests/AssemblyInfoFileUpdateTests.cs @@ -0,0 +1,23 @@ +namespace GitVersionExe.Tests +{ + using System.Collections.Generic; + using System.IO; + using GitVersion; + using NSubstitute; + using NUnit.Framework; + + [TestFixture] + public class AssemblyInfoFileUpdateTests + { + [Test] + public void ShouldStartSearchFromWorkingDirectory() + { + var fileSystem = Substitute.For(); + const string workingDir = "C:\\Testing"; + using (new AssemblyInfoFileUpdate(new Arguments{ UpdateAssemblyInfo = true }, workingDir, new Dictionary(), fileSystem)) + { + fileSystem.Received().DirectoryGetFiles(Arg.Is(workingDir), Arg.Any(), Arg.Any()); + } + } + } +} \ No newline at end of file diff --git a/GitVersionExe.Tests/GitVersionExe.Tests.csproj b/GitVersionExe.Tests/GitVersionExe.Tests.csproj index 8c1982e21f..62dcc0c050 100644 --- a/GitVersionExe.Tests/GitVersionExe.Tests.csproj +++ b/GitVersionExe.Tests/GitVersionExe.Tests.csproj @@ -39,6 +39,9 @@ ..\packages\LibGit2Sharp.0.19.0.0\lib\net40\LibGit2Sharp.dll + + ..\packages\NSubstitute.1.8.0.0\lib\net45\NSubstitute.dll + ..\packages\NUnit.2.6.3\lib\nunit.framework.dll @@ -80,6 +83,7 @@ + diff --git a/GitVersionExe.Tests/packages.config b/GitVersionExe.Tests/packages.config index f4578de3f1..b98d178f1b 100644 --- a/GitVersionExe.Tests/packages.config +++ b/GitVersionExe.Tests/packages.config @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/GitVersionExe/AssemblyInfoFileUpdate.cs b/GitVersionExe/AssemblyInfoFileUpdate.cs index 1110d67c99..72c376b440 100644 --- a/GitVersionExe/AssemblyInfoFileUpdate.cs +++ b/GitVersionExe/AssemblyInfoFileUpdate.cs @@ -9,51 +9,53 @@ class AssemblyInfoFileUpdate : IDisposable List restoreBackupTasks = new List(); List cleanupBackupTasks = new List(); - public AssemblyInfoFileUpdate(Arguments args, string workingDirectory, Dictionary variables) + public AssemblyInfoFileUpdate(Arguments args, string workingDirectory, Dictionary variables, IFileSystem fileSystem) { if (!args.UpdateAssemblyInfo) return; if (args.Output != OutputType.Json) Console.WriteLine("Updating assembly info files"); - var assemblyInfoFiles = GetAssemblyInfoFiles(workingDirectory, args); + var assemblyInfoFiles = GetAssemblyInfoFiles(workingDirectory, args, fileSystem); foreach (var assemblyInfoFile in assemblyInfoFiles) { var backupAssemblyInfo = assemblyInfoFile + ".bak"; var localAssemblyInfo = assemblyInfoFile; - File.Copy(assemblyInfoFile, backupAssemblyInfo, true); + fileSystem.Copy(assemblyInfoFile, backupAssemblyInfo, true); restoreBackupTasks.Add(() => { - if (File.Exists(localAssemblyInfo)) - File.Delete(localAssemblyInfo); - File.Move(backupAssemblyInfo, localAssemblyInfo); + if (fileSystem.Exists(localAssemblyInfo)) + fileSystem.Delete(localAssemblyInfo); + fileSystem.Move(backupAssemblyInfo, localAssemblyInfo); }); - cleanupBackupTasks.Add(() => File.Delete(backupAssemblyInfo)); + cleanupBackupTasks.Add(() => fileSystem.Delete(backupAssemblyInfo)); var assemblyVersion = string.Format("{0}.{1}.0.0", variables[VariableProvider.Major], variables[VariableProvider.Minor]); var assemblyInfoVersion = variables[VariableProvider.InformationalVersion]; var assemblyFileVersion = variables[VariableProvider.AssemblySemVer]; - var fileContents = File.ReadAllText(assemblyInfoFile) + var fileContents = fileSystem.ReadAllText(assemblyInfoFile) .RegexReplace(@"AssemblyVersion\(""\d+.\d+.\d+(.\d+|\*)?""\)", string.Format("AssemblyVersion(\"{0}\")", assemblyVersion)) .RegexReplace(@"AssemblyInformationalVersion\(""\d+.\d+.\d+(.\d+|\*)?""\)", string.Format("AssemblyInformationalVersion(\"{0}\")", assemblyInfoVersion)) .RegexReplace(@"AssemblyFileVersion\(""\d+.\d+.\d+(.\d+|\*)?""\)", string.Format("AssemblyFileVersion(\"{0}\")", assemblyFileVersion)); - File.WriteAllText(assemblyInfoFile, fileContents); + fileSystem.WriteAllText(assemblyInfoFile, fileContents); } } - static IEnumerable GetAssemblyInfoFiles(string workingDirectory, Arguments args) + static IEnumerable GetAssemblyInfoFiles(string workingDirectory, Arguments args, IFileSystem fileSystem) { if (args.UpdateAssemblyInfoFileName != null) { - if (File.Exists(args.UpdateAssemblyInfoFileName)) + var fullPath = Path.Combine(workingDirectory, args.UpdateAssemblyInfoFileName); + + if (fileSystem.Exists(fullPath)) { - return new[] { Path.GetFullPath(args.UpdateAssemblyInfoFileName) }; + return new[] { fullPath }; } } - return Directory.GetFiles(workingDirectory, "AssemblyInfo.cs", SearchOption.AllDirectories); + return fileSystem.DirectoryGetFiles(workingDirectory, "AssemblyInfo.cs", SearchOption.AllDirectories); } public void Dispose() diff --git a/GitVersionExe/FileSystem.cs b/GitVersionExe/FileSystem.cs new file mode 100644 index 0000000000..4ddd2f09bc --- /dev/null +++ b/GitVersionExe/FileSystem.cs @@ -0,0 +1,43 @@ +namespace GitVersion +{ + using System.Collections.Generic; + using System.IO; + + class FileSystem : IFileSystem + { + public void Copy(string @from, string to, bool overwrite) + { + File.Copy(from, to, overwrite); + } + + public void Move(string @from, string to) + { + File.Move(from, to); + } + + public bool Exists(string file) + { + return File.Exists(file); + } + + public void Delete(string path) + { + File.Delete(path); + } + + public string ReadAllText(string path) + { + return File.ReadAllText(path); + } + + public void WriteAllText(string file, string fileContents) + { + File.WriteAllText(file, fileContents); + } + + public IEnumerable DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption) + { + return Directory.GetFiles(directory, searchPattern, searchOption); + } + } +} \ No newline at end of file diff --git a/GitVersionExe/GitVersionExe.csproj b/GitVersionExe/GitVersionExe.csproj index eb23e12659..3a029c4cb9 100644 --- a/GitVersionExe/GitVersionExe.csproj +++ b/GitVersionExe/GitVersionExe.csproj @@ -55,8 +55,10 @@ + + diff --git a/GitVersionExe/IFileSystem.cs b/GitVersionExe/IFileSystem.cs new file mode 100644 index 0000000000..9e2a8637af --- /dev/null +++ b/GitVersionExe/IFileSystem.cs @@ -0,0 +1,16 @@ +namespace GitVersion +{ + using System.Collections.Generic; + using System.IO; + + public interface IFileSystem + { + void Copy(string from, string to, bool overwrite); + void Move(string from, string to); + bool Exists(string file); + void Delete(string path); + string ReadAllText(string path); + void WriteAllText(string file, string fileContents); + IEnumerable DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption); + } +} \ No newline at end of file diff --git a/GitVersionExe/Program.cs b/GitVersionExe/Program.cs index c8b9c63b89..b0aac61c1f 100644 --- a/GitVersionExe/Program.cs +++ b/GitVersionExe/Program.cs @@ -114,7 +114,7 @@ static int Run() } } - using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, workingDirectory, variables)) + using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, workingDirectory, variables, new FileSystem())) { var execRun = RunExecCommandIfNeeded(arguments, workingDirectory, variables); var msbuildRun = RunMsBuildIfNeeded(arguments, workingDirectory, variables);