Skip to content
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

Use hashed file contents as unique test ID #1605

Merged
merged 1 commit into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions Nodejs/Product/TestAdapter/TestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private void RunTestCases(IEnumerable<TestCase> tests, IRunContext runContext, I
}

// All tests being run are for the same test file, so just use the first test listed to get the working dir
NodejsTestInfo testInfo = new NodejsTestInfo(tests.First().FullyQualifiedName);
NodejsTestInfo testInfo = new NodejsTestInfo(tests.First().FullyQualifiedName, tests.First().CodeFilePath);
var workingDir = Path.GetDirectoryName(CommonUtils.GetAbsoluteFilePath(settings.WorkingDir, testInfo.ModulePath));

foreach (var test in tests) {
Expand Down Expand Up @@ -314,7 +314,7 @@ select connection.LocalEndPoint.Port
}

private IEnumerable<string> GetInterpreterArgs(TestCase test, string workingDir, string projectRootDir) {
TestFrameworks.NodejsTestInfo testInfo = new TestFrameworks.NodejsTestInfo(test.FullyQualifiedName);
TestFrameworks.NodejsTestInfo testInfo = new TestFrameworks.NodejsTestInfo(test.FullyQualifiedName, test.CodeFilePath);
TestFrameworks.FrameworkDiscover discover = new TestFrameworks.FrameworkDiscover();
return discover.Get(testInfo.TestFramework).ArgumentsToRunTests(testInfo.TestName, testInfo.ModulePath, workingDir, projectRootDir);
}
Expand Down
41 changes: 38 additions & 3 deletions Nodejs/Product/TestAdapter/TestFrameworks/NodejsTestInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,70 @@
//*********************************************************//

using System;
using System.IO;
using System.Security.Cryptography;

namespace Microsoft.NodejsTools.TestAdapter.TestFrameworks {
class NodejsTestInfo {
public NodejsTestInfo(string fullyQualifiedName) {
public NodejsTestInfo(string fullyQualifiedName, string modulePath) {
string[] parts = fullyQualifiedName.Split(new string[] { "::" }, StringSplitOptions.None);
if (parts.Length != 3) {
throw new ArgumentException("Invalid fully qualified test name");
}
ModulePath = parts[0];
ModulePath = modulePath;
ModuleName = parts[0];
TestName = parts[1];
TestFramework = parts[2];
}

public NodejsTestInfo(string modulePath, string testName, string testFramework, int line, int column)
{
ModulePath = modulePath;
SetModuleName(ModulePath);
TestName = testName;
TestFramework = testFramework;
SourceLine = line;
SourceColumn = column;
}

private void SetModuleName(string modulePath)
{
ModuleName = String.Format("{0}[{1}]",
(string)Path.GetFileName(modulePath).Split('.').GetValue(0),
GetHash(modulePath));
}

private string GetHash(string filePath)
{
try
{
using (FileStream stream = File.OpenRead(filePath))
{
SHA1Managed sha = new SHA1Managed();
byte[] hash = sha.ComputeHash(stream);

// chop hash in half since we just need a unique ID
Int32 startIndex = hash.Length / 2;
sha.Dispose();

return BitConverter.ToString(hash, startIndex).Replace("-", String.Empty);
}
} catch (FileNotFoundException)
{
// Just return some default value and let node handle it later
return "FILE_NOT_FOUND";
}
}

public string FullyQualifiedName {
get {
return ModulePath + "::" + TestName + "::" + TestFramework;
return ModuleName + "::" + TestName + "::" + TestFramework;
}
}
public string ModulePath { get; private set; }

public string ModuleName { get; private set; }

public string TestName { get; private set; }

public string TestFramework { get; private set; }
Expand Down
5 changes: 3 additions & 2 deletions Nodejs/Tests/TestAdapterTests/NodejsTestInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void ConstructFullyQualifiedName_ValidInput() {
//Act
NodejsTestInfo testInfo = new NodejsTestInfo(testFile, testName, testFramework, 0, 0);
//Assert
string expected = testFile + "::" + testName + "::" + testFramework;
string expected = testInfo.ModuleName + "::" + testName + "::" + testFramework;
Assert.AreEqual(expected, testInfo.FullyQualifiedName);
Assert.AreEqual(testName, testInfo.TestName);
Assert.AreEqual(testFramework, testInfo.TestFramework);
Expand All @@ -29,9 +29,10 @@ public void ConstructFullyQualifiedName_ValidInput() {
public void ConstructFromQualifiedName_ThrowOnInValidInput() {
//Arrange
string badDummy = "c:\\dummy.js::dummy::dumm2::test1";
string dummyPath = "c:\\dummyTest.js";

//Act
NodejsTestInfo testInfo = new NodejsTestInfo(badDummy);
NodejsTestInfo testInfo = new NodejsTestInfo(badDummy, dummyPath);

//Assert: N/A
}
Expand Down