diff --git a/GraphBuilder/src/Graph.cs b/GraphBuilder/src/Graph.cs index d92b02a..7aa6dac 100644 --- a/GraphBuilder/src/Graph.cs +++ b/GraphBuilder/src/Graph.cs @@ -5,45 +5,76 @@ namespace PackageAnalyzer { public class Graph { - private Dictionary> nodes; - + private Dictionary> directednodes; + private Dictionary> parentnodes; public Graph() { - nodes = new Dictionary>(); + directednodes = new Dictionary>(); + parentnodes = new Dictionary>(); + } + + public List Paths(string V) + { + return directednodes[V]; } public int NodeCount { - get { return nodes.Keys.Count; } + get { return directednodes.Keys.Count; } } - public List Edges(string node) + public List Children(string node) { - return nodes[node]; + return parentnodes[node]; } public void AddEdge(string V) { - if (nodes.ContainsKey(V)) + if (!directednodes.ContainsKey(V)) { - return; + directednodes.Add(V, new List()); } - nodes.Add(V, new List()); + if (!parentnodes.ContainsKey(V)) + { + parentnodes.Add(V, new List()); + } + } public void AddEdge(string V, string E) { - if (nodes.ContainsKey(V)) + // Add vertex and edge information + // Example: + // A <= B + // B is the vertex and A is the edge between B and A + // A is also the parent of B + if (directednodes.ContainsKey(V)) { - nodes[V].Add(E); + directednodes[V].Add(E); } else { - nodes.Add(V, new List()); - nodes[V].Add(E); + directednodes.Add(V, new List()); + directednodes[V].Add(E); } + // If this vertex has not been seen yet also store it as a possible parent + if(!parentnodes.ContainsKey(V)) + { + parentnodes.Add(V, new List()); + } + + // Add the edge and vertex parent information + if (parentnodes.ContainsKey(E)) + { + parentnodes[E].Add(V); + } + else + { + parentnodes.Add(E, new List()); + parentnodes[E].Add(V); + } } private bool IsCyclicInternal(string V, Dictionary visited, Dictionary recursive) @@ -53,7 +84,7 @@ private bool IsCyclicInternal(string V, Dictionary visited, Dictio visited[V] = true; recursive[V] = true; - foreach (string E in nodes[V]) + foreach (string E in directednodes[V]) { if (!visited[E] && IsCyclicInternal(E, visited, recursive)) { @@ -80,13 +111,13 @@ public bool IsCyclic Dictionary recursive = new Dictionary(); // initialize the search... - foreach (string V in nodes.Keys) + foreach (string V in directednodes.Keys) { visited.Add(V,false); recursive.Add(V,false); } - foreach (string V in nodes.Keys) + foreach (string V in directednodes.Keys) { if (IsCyclicInternal(V, visited, recursive)) { diff --git a/GraphBuilder/tests/Graph.Tests.cs b/GraphBuilder/tests/Graph.Tests.cs index cbe80a1..fd01ef7 100644 --- a/GraphBuilder/tests/Graph.Tests.cs +++ b/GraphBuilder/tests/Graph.Tests.cs @@ -19,15 +19,20 @@ public void SimpleGraphCycleTest2() GraphBuilder gb = GraphTestUtilities.PopulateGB("simplecycle2"); Assert.IsFalse(gb.Graph.IsCyclic); + Assert.AreEqual(1, gb.Graph.Paths("A").Count); + Assert.IsTrue(gb.Graph.Paths("A").Contains("B")); + } [TestMethod] - public void SimplGraphCycleTest3() + public void SimpleGraphCycleTest3() { GraphBuilder gb = GraphTestUtilities.PopulateGB("simplecycle3"); Assert.IsTrue(gb.Graph.IsCyclic); + Assert.AreEqual(1, gb.Graph.Paths("B").Count); + Assert.IsTrue(gb.Graph.Paths("B").Contains("C")); } [TestMethod] @@ -44,6 +49,21 @@ public void IntermediateGraphCycleTest1() GraphBuilder gb = GraphTestUtilities.PopulateGB("intermediatecycle1"); Assert.IsFalse(gb.Graph.IsCyclic); + + Assert.AreEqual(2, gb.Graph.Children("A").Count); + Assert.IsTrue(gb.Graph.Children("A").Contains("B")); + Assert.IsTrue(gb.Graph.Children("A").Contains("C")); + + Assert.AreEqual(0, gb.Graph.Children("B").Count); + + Assert.AreEqual(2, gb.Graph.Children("C").Count); + Assert.IsTrue(gb.Graph.Children("C").Contains("D")); + Assert.IsTrue(gb.Graph.Children("C").Contains("E")); + + Assert.AreEqual(1, gb.Graph.Children("D").Count); + Assert.IsTrue(gb.Graph.Children("D").Contains("E")); + + Assert.AreEqual(0, gb.Graph.Children("E").Count); } } } diff --git a/GraphBuilder/tests/GraphBuilder.Tests.cs b/GraphBuilder/tests/GraphBuilder.Tests.cs index fdcc034..16bdb24 100644 --- a/GraphBuilder/tests/GraphBuilder.Tests.cs +++ b/GraphBuilder/tests/GraphBuilder.Tests.cs @@ -24,7 +24,9 @@ public void SimpleGraphBuilderTest1() Assert.AreEqual(1, gb.Graph.NodeCount); - Assert.AreEqual(0, gb.Graph.Edges("A").Count); + Assert.AreEqual(0, gb.Graph.Paths("A").Count); + + Assert.AreEqual(0, gb.Graph.Children("A").Count); } @@ -35,10 +37,11 @@ public void SimpleGraphBuilderTest2() Assert.AreEqual(2, gb.Graph.NodeCount); - Assert.AreEqual(1, gb.Graph.Edges("A").Count); + Assert.AreEqual(1, gb.Graph.Paths("A").Count); - Assert.AreEqual(0, gb.Graph.Edges("B").Count); + Assert.AreEqual(0, gb.Graph.Paths("B").Count); + Assert.AreEqual(1, gb.Graph.Children("B").Count); } [TestMethod] @@ -48,11 +51,19 @@ public void SimpleGraphBuilderTest3() Assert.AreEqual(3, gb.Graph.NodeCount); - Assert.AreEqual(2, gb.Graph.Edges("A").Count); + Assert.AreEqual(2, gb.Graph.Paths("A").Count); + + Assert.AreEqual(0, gb.Graph.Paths("B").Count); + + Assert.AreEqual(0, gb.Graph.Paths("C").Count); + + Assert.IsTrue(gb.Graph.Paths("A").Contains("B")); + + Assert.IsTrue(gb.Graph.Paths("A").Contains("C")); - Assert.AreEqual(0, gb.Graph.Edges("B").Count); + Assert.AreEqual(1, gb.Graph.Children("B").Count); - Assert.AreEqual(0, gb.Graph.Edges("C").Count); + Assert.AreEqual(1, gb.Graph.Children("C").Count); } diff --git a/PackageBuilder/src/PackageBuilder.cs b/PackageBuilder/src/PackageBuilder.cs index 18aa1bb..1548921 100644 --- a/PackageBuilder/src/PackageBuilder.cs +++ b/PackageBuilder/src/PackageBuilder.cs @@ -1,36 +1,139 @@ using System; using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.IO.Compression; namespace PackageAnalyzer { public class PackageBuilder { // location relative to specified rootFolder. - const string PACKAGE_DESCRIPTIONS = "//packages//descriptions"; - const string PACKAGE_CACHE = "//packages//cache"; - const string PACKAGE_SOURCE = "//src"; - const string PACKAGE_OUTPUT = "//output"; + const string PACKAGE_DESCRIPTIONS = @"\packages\descriptions\"; + const string PACKAGE_CACHE = @"\packages\cache\"; + const string PACKAGE_SOURCE = @"\src\"; + const string PACKAGE_OUTPUT = @"\output\"; + const string PACKAGE_EXTENSION = ".zip"; + string _cacheRoot; + string _sourceRoot; + string _outputRoot; + string _packageRoot; - - - public PackageBuilder() + List _cacheContents; + public PackageBuilder(string rootFolder) { + _cacheRoot = $"{rootFolder}{PACKAGE_CACHE}"; + _sourceRoot = $"{rootFolder}{PACKAGE_SOURCE}"; + _outputRoot = $"{rootFolder}{PACKAGE_OUTPUT}"; + _packageRoot = $"{rootFolder}{PACKAGE_DESCRIPTIONS}"; + + // create output dir if not exist. + if(!Directory.Exists(_outputRoot)) + { + Directory.CreateDirectory(_outputRoot); + } + + // initialize cache contents + DirectoryInfo di = new DirectoryInfo(_cacheRoot); + _cacheContents = di.GetFiles().Select(f => f.Name.Substring(0,f.Name.Length - PACKAGE_EXTENSION.Length)).ToList(); } + public Dictionary Build(string startNode, string sourceFolder, Dictionary hash, Graph g) + { + List packages = new List(); + packages.Add(startNode); + + Dictionary packageBuildResults = new Dictionary(); + + BuildPackages(packages, hash, g, packageBuildResults); - public List Build(string startNode, string rootFolder) + return packageBuildResults; + } + + private void BuildPackages(List packages, Dictionary hash, Graph g, Dictionary buildResults) { - List builtPackages = new List(); + if (packages.Count == 0) + { + return; + } + + List children = new List(); + + // if the parent built then this needs to build + // if the hash version doesn't exist in cache the this needs to build + // if the hash version exists in cache then copy to output + + bool forceBuild; + + foreach (string package in packages) + { + bool packagebuilt; + buildResults.TryGetValue(package, out packagebuilt); + if (packagebuilt) + { + continue; + } + + forceBuild = false; + + // if the packages dependency was built then this package will beuilt too. + foreach (string k in buildResults.Keys) + { + if (g.Paths(package).Contains(k) && buildResults[k]) + { + forceBuild = true; + } + } + + buildResults.Add(package, BuildPackage(package, hash, forceBuild)); + + children.AddRange(g.Children(package)); + } - GraphBuilder gb = new GraphBuilder($"{rootFolder}{PACKAGE_DESCRIPTIONS}"); + BuildPackages(children, hash, g, buildResults); + } - if (gb.Graph.IsCyclic) + private bool BuildPackage(string package, Dictionary hash, bool force) + { + // in this context all building means is getting a zip version of the package with hash extention to the output folder. + // this can happen 1 of 2 ways. 1) copy the existing one from cache or 2) zip the src add a new one to cache and output + + string packageHashName = $"{hash[package]}"; + if (!PackageExists(packageHashName) || force) + { + Compile(package, packageHashName); + CopyOutputToCache(packageHashName); + return true; // true because package did 'build' + } + else { - // need logging. throw cycle exception - throw new Exceptions.PackageBuilderCycleException("Cycle detected in package graph."); + CopyCacheToOuput(packageHashName); + return false; // false because package didn't 'build' } - return builtPackages; + } + + private bool PackageExists(string packageHashName) + { + return _cacheContents.Contains(packageHashName); + } + + private void CopyCacheToOuput(string packageHashName) + { + File.Copy($"{_cacheRoot}{packageHashName}.zip", $"{_outputRoot}{packageHashName}.zip", true); + } + + private void CopyOutputToCache(string packageHashName) + { + File.Copy($"{_outputRoot}{packageHashName}.zip", $"{_cacheRoot}{packageHashName}.zip", true); + } + + private void Compile(string package, string packageHashName) + { + string source = $"{_sourceRoot}{package}"; + string dest = $"{_outputRoot}{packageHashName}.zip"; + + ZipFile.CreateFromDirectory($"{_sourceRoot}{package}", $"{_outputRoot}{packageHashName}.zip"); } } } diff --git a/PackageBuilder/tests/PackageBuilder.Tests.cs b/PackageBuilder/tests/PackageBuilder.Tests.cs index f392852..e79fab5 100644 --- a/PackageBuilder/tests/PackageBuilder.Tests.cs +++ b/PackageBuilder/tests/PackageBuilder.Tests.cs @@ -1,24 +1,232 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System.IO; using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; namespace PackageAnalyzer.Tests { [TestClass] public class PackageBuilderTests { + string packagedescriptions = @"\packages\descriptions"; + string packagesource = @"\src"; + + private void CleanOutput(string testroot) + { + foreach (string f in Directory.GetFiles($"{testroot}\\output")) + { + File.Delete(f); + } + } + + private void CleanCache(string testroot, List excludeList = null) + { + foreach (string f in Directory.GetFiles($"{testroot}\\packages\\cache")) + { + if (excludeList == null) + { + File.Delete(f); + } + else + { + if(!excludeList.Any(e => f.Contains(e))) + { + File.Delete(f); + } + } + } + } + [TestMethod] public void SimpleBuilderTest1() { - PackageBuilder pb = new PackageBuilder(); + string cwd = Directory.GetCurrentDirectory(); + string testroot = $"{cwd}\\testcases\\simple1"; + + GraphBuilder gb = new GraphBuilder($"{testroot}{packagedescriptions}"); + + DirectoryInfo di = new DirectoryInfo($"{testroot}{packagesource}"); + List paths = di.GetDirectories().Select(d => d.FullName).ToList(); + var task = Task.Run(async () => await PackageHasher.HashFoldersAsync(paths, testroot)); + + // package source to package source hash mapping + Dictionary packageSourceHash = task.Result; + + CleanCache(testroot); + + { + CleanOutput(testroot); + PackageBuilder pb = new PackageBuilder(testroot); + Dictionary buildResults = pb.Build("A", $"{testroot}{packagesource}", packageSourceHash, gb.Graph); + Assert.IsTrue(buildResults["A"]); + } + + { + CleanOutput(testroot); + PackageBuilder pb = new PackageBuilder(testroot); + Dictionary buildResults = pb.Build("A", $"{testroot}{packagesource}", packageSourceHash, gb.Graph); + Assert.IsFalse(buildResults["A"]); + } + } + + [TestMethod] + public void SimpleBuilderTest2() + { + string cwd = Directory.GetCurrentDirectory(); + string testroot = $"{cwd}\\testcases\\simple2"; + + GraphBuilder gb = new GraphBuilder($"{testroot}{packagedescriptions}"); + + DirectoryInfo di = new DirectoryInfo($"{testroot}{packagesource}"); + List paths = di.GetDirectories().Select(d => d.FullName).ToList(); + var task = Task.Run(async () => await PackageHasher.HashFoldersAsync(paths, testroot)); + + // package source to package source hash mapping + Dictionary packageSourceHash = task.Result; + + CleanCache(testroot); + + { + CleanOutput(testroot); + PackageBuilder pb = new PackageBuilder(testroot); + Dictionary buildResults = pb.Build("A", $"{testroot}{packagesource}", packageSourceHash, gb.Graph); + Assert.IsTrue(buildResults["A"]); + Assert.IsTrue(buildResults["B"]); + } + } + + [TestMethod] + public void SimpleBuilderTest3() + { + // + // A, B, and C should build + // + + string cwd = Directory.GetCurrentDirectory(); + string testroot = $"{cwd}\\testcases\\simple3"; + + GraphBuilder gb = new GraphBuilder($"{testroot}{packagedescriptions}"); + + DirectoryInfo di = new DirectoryInfo($"{testroot}{packagesource}"); + List paths = di.GetDirectories().Select(d => d.FullName).ToList(); + var task = Task.Run(async () => await PackageHasher.HashFoldersAsync(paths, testroot)); + + // package source to package source hash mapping + Dictionary packageSourceHash = task.Result; + + CleanCache(testroot); + + { + CleanOutput(testroot); + PackageBuilder pb = new PackageBuilder(testroot); + Dictionary buildResults = pb.Build("A", $"{testroot}{packagesource}", packageSourceHash, gb.Graph); + Assert.IsTrue(buildResults["A"]); + Assert.IsTrue(buildResults["B"]); + Assert.IsTrue(buildResults["C"]); + } + } + + [TestMethod] + public void SimpleBuilderTest4() + { + // + // cache contains a pre built A. A does not build. B and C do. + // + + string cwd = Directory.GetCurrentDirectory(); + string testroot = $"{cwd}\\testcases\\simple4"; + + GraphBuilder gb = new GraphBuilder($"{testroot}{packagedescriptions}"); + + DirectoryInfo di = new DirectoryInfo($"{testroot}{packagesource}"); + List paths = di.GetDirectories().Select(d => d.FullName).ToList(); + var task = Task.Run(async () => await PackageHasher.HashFoldersAsync(paths, testroot)); + + // package source to package source hash mapping + Dictionary packageSourceHash = task.Result; + + List excludeList = new List(); + excludeList.Add(packageSourceHash["A"]); + CleanCache(testroot, excludeList); + { + CleanOutput(testroot); + PackageBuilder pb = new PackageBuilder(testroot); + Dictionary buildResults = pb.Build("A", $"{testroot}{packagesource}", packageSourceHash, gb.Graph); + Assert.IsFalse(buildResults["A"]); + Assert.IsTrue(buildResults["B"]); + Assert.IsTrue(buildResults["C"]); + } + } + + [TestMethod] + public void SimpleBuilderTest5() + { + // + // cache contains a pre built A and B. A and B do not build. C will build. + // + + string cwd = Directory.GetCurrentDirectory(); + string testroot = $"{cwd}\\testcases\\simple5"; + + GraphBuilder gb = new GraphBuilder($"{testroot}{packagedescriptions}"); + + DirectoryInfo di = new DirectoryInfo($"{testroot}{packagesource}"); + List paths = di.GetDirectories().Select(d => d.FullName).ToList(); + var task = Task.Run(async () => await PackageHasher.HashFoldersAsync(paths, testroot)); + + // package source to package source hash mapping + Dictionary packageSourceHash = task.Result; + + List excludeList = new List(); + excludeList.Add(packageSourceHash["A"]); + excludeList.Add(packageSourceHash["B"]); + CleanCache(testroot, excludeList); + + { + CleanOutput(testroot); + PackageBuilder pb = new PackageBuilder(testroot); + Dictionary buildResults = pb.Build("A", $"{testroot}{packagesource}", packageSourceHash, gb.Graph); + Assert.IsFalse(buildResults["A"]); + Assert.IsFalse(buildResults["B"]); + Assert.IsTrue(buildResults["C"]); + } + } + + [TestMethod] + public void IntermediateBuilderTest1() + { + // + // cache contains a pre built A and B. A and B do not build. C will build. + // string cwd = Directory.GetCurrentDirectory(); + string testroot = $"{cwd}\\testcases\\intermediate1"; + + GraphBuilder gb = new GraphBuilder($"{testroot}{packagedescriptions}"); - DirectoryInfo di = new DirectoryInfo($"{cwd}\\testcases\\simple1"); + DirectoryInfo di = new DirectoryInfo($"{testroot}{packagesource}"); + List paths = di.GetDirectories().Select(d => d.FullName).ToList(); + var task = Task.Run(async () => await PackageHasher.HashFoldersAsync(paths, testroot)); - List buildResults = pb.Build("A", di.FullName); + // package source to package source hash mapping + Dictionary packageSourceHash = task.Result; - Assert.AreEqual(0, buildResults.Count); + List excludeList = new List(); + //excludeList.Add(packageSourceHash["A"]); + //excludeList.Add(packageSourceHash["B"]); + CleanCache(testroot, excludeList); + + { + CleanOutput(testroot); + PackageBuilder pb = new PackageBuilder(testroot); + Dictionary buildResults = pb.Build("A", $"{testroot}{packagesource}", packageSourceHash, gb.Graph); + Assert.IsTrue(buildResults["A"]); + Assert.IsTrue(buildResults["B"]); + Assert.IsTrue(buildResults["C"]); + Assert.IsTrue(buildResults["D"]); + Assert.IsTrue(buildResults["E"]); + } } } } diff --git a/PackageBuilder/tests/testcases/intermediate1/src/A/file1.txt b/PackageBuilder/tests/testcases/intermediate1/src/A/file1.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/intermediate1/src/A/file1.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/intermediate1/src/B/file2.txt b/PackageBuilder/tests/testcases/intermediate1/src/B/file2.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/intermediate1/src/B/file2.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/intermediate1/src/C/file3.txt b/PackageBuilder/tests/testcases/intermediate1/src/C/file3.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/intermediate1/src/C/file3.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/intermediate1/src/D/file4.txt b/PackageBuilder/tests/testcases/intermediate1/src/D/file4.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/intermediate1/src/D/file4.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/intermediate1/src/E/file5.txt b/PackageBuilder/tests/testcases/intermediate1/src/E/file5.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/intermediate1/src/E/file5.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/simple1/src/A/file1.txt b/PackageBuilder/tests/testcases/simple1/src/A/file1.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/simple1/src/A/file1.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/simple2/src/A/file1.txt b/PackageBuilder/tests/testcases/simple2/src/A/file1.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/simple2/src/A/file1.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/simple2/src/B/file2.txt b/PackageBuilder/tests/testcases/simple2/src/B/file2.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/simple2/src/B/file2.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/simple3/src/A/file1.txt b/PackageBuilder/tests/testcases/simple3/src/A/file1.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/simple3/src/A/file1.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/simple3/src/B/file2.txt b/PackageBuilder/tests/testcases/simple3/src/B/file2.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/simple3/src/B/file2.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/simple3/src/C/file3.txt b/PackageBuilder/tests/testcases/simple3/src/C/file3.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/simple3/src/C/file3.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/simple4/src/A/file1.txt b/PackageBuilder/tests/testcases/simple4/src/A/file1.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/simple4/src/A/file1.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/simple4/src/B/file2.txt b/PackageBuilder/tests/testcases/simple4/src/B/file2.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/simple4/src/B/file2.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/simple4/src/C/file3.txt b/PackageBuilder/tests/testcases/simple4/src/C/file3.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/simple4/src/C/file3.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/simple5/src/A/file1.txt b/PackageBuilder/tests/testcases/simple5/src/A/file1.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/simple5/src/A/file1.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/simple5/src/B/file2.txt b/PackageBuilder/tests/testcases/simple5/src/B/file2.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/simple5/src/B/file2.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/testcases/simple5/src/C/file3.txt b/PackageBuilder/tests/testcases/simple5/src/C/file3.txt new file mode 100644 index 0000000..86fc6b2 --- /dev/null +++ b/PackageBuilder/tests/testcases/simple5/src/C/file3.txt @@ -0,0 +1 @@ +test data for file1. Just needed to get a hash value. \ No newline at end of file diff --git a/PackageBuilder/tests/tests.csproj b/PackageBuilder/tests/tests.csproj index 0efa5da..b2824d3 100644 --- a/PackageBuilder/tests/tests.csproj +++ b/PackageBuilder/tests/tests.csproj @@ -17,8 +17,14 @@ + + - + + + + + diff --git a/PackageHasher/src/PackageHasher.cs b/PackageHasher/src/PackageHasher.cs index 87ea1c3..8e3deb2 100644 --- a/PackageHasher/src/PackageHasher.cs +++ b/PackageHasher/src/PackageHasher.cs @@ -10,7 +10,7 @@ namespace PackageAnalyzer { public class PackageHasher { - public async Task> HashFoldersAsync(List folders, string rootPath) + public static async Task> HashFoldersAsync(List folders, string rootPath) { Dictionary hashedFolders = new Dictionary(); @@ -34,7 +34,7 @@ public async Task> HashFoldersAsync(List fold } - async Task<(string folder, string folderwithhash)> HashFolderAsync(DirectoryInfo folder, string rootPath = "", string searchPattern = "*", SearchOption searchOption = SearchOption.AllDirectories) + static async Task<(string folder, string folderwithhash)> HashFolderAsync(DirectoryInfo folder, string rootPath = "", string searchPattern = "*", SearchOption searchOption = SearchOption.AllDirectories) { using(var alg = MD5.Create()) { diff --git a/PackageHasher/tests/PackageHaser.Tests.cs b/PackageHasher/tests/PackageHaser.Tests.cs index f602ee9..dbb0545 100644 --- a/PackageHasher/tests/PackageHaser.Tests.cs +++ b/PackageHasher/tests/PackageHaser.Tests.cs @@ -13,15 +13,13 @@ public class PackageHasherTests [TestMethod] public void SimpleHashTest1() { - PackageHasher ph = new PackageHasher(); - string cwd = Directory.GetCurrentDirectory(); string root = $"{cwd}\\testcases\\simple1"; List paths = new List(); paths.Add($"{root}\\package1"); - var task = Task.Run(async () => await ph.HashFoldersAsync(paths, root)); + var task = Task.Run(async () => await PackageHasher.HashFoldersAsync(paths, root)); var result = task.Result; @@ -32,8 +30,6 @@ public void SimpleHashTest1() [TestMethod] public void SimpleHashTest2() { - PackageHasher ph = new PackageHasher(); - string cwd = Directory.GetCurrentDirectory(); string root = $"{cwd}\\testcases\\simple2"; @@ -41,7 +37,7 @@ public void SimpleHashTest2() paths.Add($"{root}\\package1"); paths.Add($"{root}\\package2"); - var task = Task.Run(async () => await ph.HashFoldersAsync(paths, root)); + var task = Task.Run(async () => await PackageHasher.HashFoldersAsync(paths, root)); var result = task.Result; @@ -54,7 +50,6 @@ public void SimpleHashTest2() public void CompareHashTest1() { // compare hash of same folder and contents from 2 different roots. Should have the smae hash value. - PackageHasher ph = new PackageHasher(); string cwd = Directory.GetCurrentDirectory(); string root1 = $"{cwd}\\testcases\\simple1"; @@ -66,10 +61,10 @@ public void CompareHashTest1() List paths2 = new List(); paths2.Add($"{root2}\\package1"); - var task1 = Task.Run(async () => await ph.HashFoldersAsync(paths1, root1)); + var task1 = Task.Run(async () => await PackageHasher.HashFoldersAsync(paths1, root1)); var result1 = task1.Result; - var task2 = Task.Run(async () => await ph.HashFoldersAsync(paths2, root2)); + var task2 = Task.Run(async () => await PackageHasher.HashFoldersAsync(paths2, root2)); var result2 = task2.Result; Assert.AreEqual(result1["package1"], result2["package1"]);