Skip to content

Commit

Permalink
add package hasher - working hash
Browse files Browse the repository at this point in the history
  • Loading branch information
hlotyaks committed Jan 3, 2020
1 parent f7ae1f6 commit 097151b
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 23 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ jobs:
uses: actions/setup-dotnet@v1
with:
dotnet-version: 2.2.108
- name: Build with dotnet
- name: Build - GraphBuilder
run: dotnet build GraphBuilder/GraphBuilder.sln -c Release
- name: Run tests
- name: Build - PackageHasher
run: dotnet build PackageHasher/PackageHasher.sln -c Release
- name: Test - GraphBuilder
run: dotnet test GraphBuilder/tests/tests.csproj -c Release -v m
- name: Test - PackageHasher
run: dotnet test PackageHasher/tests/tests.csproj -c Release -v m
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"args": [
"build",
// project
"${workspaceFolder}\\GraphBuilder\\src\\GraphBuilder.csproj"
"${workspaceFolder}\\GraphBuilder\\src\\GraphBuilder.csproj",
// Ask dotnet build to generate full paths for file names.
"/property:GenerateFullPaths=true",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
Expand Down
2 changes: 1 addition & 1 deletion GraphBuilder/tests/Graph.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PackageAnalyzer.Graph.Tests
namespace PackageAnalyzer.Tests
{
[TestClass]
public class GraphTests
Expand Down
2 changes: 1 addition & 1 deletion GraphBuilder/tests/GraphBuilder.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using PackageAnalyzer;
using System.IO;

namespace PackageAnalyzer.Graph.Tests
namespace PackageAnalyzer.Tests
{
[TestClass]
public class GraphBuilderTests
Expand Down
2 changes: 1 addition & 1 deletion GraphBuilder/tests/GraphUtilities.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.IO;

namespace PackageAnalyzer.Graph.Tests
namespace PackageAnalyzer.Tests
{
public class GraphTestUtilities
{
Expand Down
48 changes: 48 additions & 0 deletions PackageHasher/PackageHasher.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PackageHasher", "src\PackageHasher.csproj", "{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tests", "tests\tests.csproj", "{3679A64B-8D42-400B-996B-8735D9A9C16C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Debug|x64.ActiveCfg = Debug|Any CPU
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Debug|x64.Build.0 = Debug|Any CPU
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Debug|x86.ActiveCfg = Debug|Any CPU
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Debug|x86.Build.0 = Debug|Any CPU
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Release|Any CPU.Build.0 = Release|Any CPU
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Release|x64.ActiveCfg = Release|Any CPU
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Release|x64.Build.0 = Release|Any CPU
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Release|x86.ActiveCfg = Release|Any CPU
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Release|x86.Build.0 = Release|Any CPU
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Debug|x64.ActiveCfg = Debug|Any CPU
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Debug|x64.Build.0 = Debug|Any CPU
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Debug|x86.ActiveCfg = Debug|Any CPU
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Debug|x86.Build.0 = Debug|Any CPU
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Release|Any CPU.Build.0 = Release|Any CPU
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Release|x64.ActiveCfg = Release|Any CPU
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Release|x64.Build.0 = Release|Any CPU
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Release|x86.ActiveCfg = Release|Any CPU
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
41 changes: 28 additions & 13 deletions PackageHasher/src/PackageHasher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,57 @@
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Linq;
using System.Collections.Generic;

namespace PackageAnalyzer
{
public class PackageHasher
{
public Dictionary<string, string> HashFolders(List<string> folders)
public async Task<Dictionary<string, string>> HashFoldersAsync(List<string> folders, string rootPath)
{
Dictionary<string, string> hasedFolders = new Dictionary<string, string>();
Dictionary<string, string> hashedFolders = new Dictionary<string, string>();

IEnumerable<Task<(string folder, string folderwithhash)>> hashFolderTasksQuery =
folders.Select( f => HashFolderAsync(new DirectoryInfo(f), rootPath));

return hasedFolders;
}
async Task<string> HashFolder(DirectoryInfo folder, bool excludeRoot, string searchPattern = "*", SearchOption searchOption = SearchOption.AllDirectories)
{
string rootPath = String.Empty;
List<Task<(string folder, string folderwithhash)>> hashFolderTasks = hashFolderTasksQuery.ToList();

if(excludeRoot)
while (hashFolderTasks.Count > 0)
{
rootPath = folder.FullName.Substring(0, folder.FullName.Length - folder.FullName.Split('\\').Last().Length );
}
Task<(string folder, string folderwithhash)> firstTask = await Task.WhenAny(hashFolderTasks);

hashFolderTasks.Remove(firstTask);

(string folder, string folderwithhash) HashFolderValue = await firstTask;

hashedFolders.Add(HashFolderValue.folder, HashFolderValue.folderwithhash);
}

return hashedFolders;
}


async Task<(string folder, string folderwithhash)> HashFolderAsync(DirectoryInfo folder, string rootPath = "", string searchPattern = "*", SearchOption searchOption = SearchOption.AllDirectories)
{
using(var alg = MD5.Create())
{

var result = await alg.ComputeHashAsync(folder.EnumerateFiles(searchPattern, searchOption), rootPath);

// Folder has starts with folder name.
// Format - name.hash
StringBuilder sb = new StringBuilder();
sb.Append(folder.Name);
sb.Append(".");

// Build the final string by converting each byte
// into hex and appending it to a StringBuilder
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
sb.Append(result[i].ToString("X2"));
}

// And return it
return sb.ToString();
return (folder: folder.Name, folderwithhash: sb.ToString());

}
}
Expand Down
43 changes: 41 additions & 2 deletions PackageHasher/tests/PackageHaser.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PackageAnalyzer;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace PackageAnalyzer.PackageHasher.Tests
namespace PackageAnalyzer.Tests
{

[TestClass]
public class PackageHasherTests
{
[TestMethod]
public void TestMethod1()
public void SimpleTest1()
{
PackageHasher ph = new PackageHasher();

string cwd = Directory.GetCurrentDirectory();
string root = $"{cwd}\\testcases\\simple2";

List<string> paths = new List<string>();
paths.Add($"{root}\\package1");

var task = Task.Run(async () => await ph.HashFoldersAsync(paths, root));

var result = task.Result;

Assert.AreEqual(1, result.Keys.Count);
Assert.IsTrue(result.ContainsKey("package1"));
}

[TestMethod]
public void SimpleTest2()
{
PackageHasher ph = new PackageHasher();

string cwd = Directory.GetCurrentDirectory();
string root = $"{cwd}\\testcases\\simple2";

List<string> paths = new List<string>();
paths.Add($"{root}\\package1");
paths.Add($"{root}\\package2");

var task = Task.Run(async () => await ph.HashFoldersAsync(paths, root));

var result = task.Result;

Assert.AreEqual(2, result.Keys.Count);
Assert.IsTrue(result.ContainsKey("package1"));
Assert.IsTrue(result.ContainsKey("package2"));
}
}

Expand Down
1 change: 1 addition & 0 deletions PackageHasher/tests/testcases/simple1/package1/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test data for file1. Just needed to get a hash value.
1 change: 1 addition & 0 deletions PackageHasher/tests/testcases/simple2/package1/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test data for file1. Just needed to get a hash value.
1 change: 1 addition & 0 deletions PackageHasher/tests/testcases/simple2/package2/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test data for file1. Just needed to get a hash value.
8 changes: 8 additions & 0 deletions PackageHasher/tests/tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\PackageHasher.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="testcases\**\*.*" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Welcome to the Package Analyzer Project
This repository contains the source code for:
* GraphBuilder - constructs a directed graph from xml files. Identifies cycles in the graph.

* PackageHasher - Hashes contents of folders to be used in identifying changes.

## Project Build Status

Project|Build Status
---|---
GraphBuilder|![Build Status](https://github.com/hlotyaks/PackageAnalyzer/workflows/.NET%20Core/badge.svg)
PackageAnalyzer|![Build Status](https://github.com/hlotyaks/PackageAnalyzer/workflows/.NET%20Core/badge.svg)

0 comments on commit 097151b

Please sign in to comment.