Skip to content

Commit aff39e9

Browse files
authored
Merge pull request #1605 from ozyx/mocha-unique-id
Use hashed file contents as unique test ID
2 parents 1032c44 + 8e9cb40 commit aff39e9

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

Nodejs/Product/TestAdapter/TestExecutor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ private void RunTestCases(IEnumerable<TestCase> tests, IRunContext runContext, I
214214
}
215215

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

220220
foreach (var test in tests) {
@@ -314,7 +314,7 @@ select connection.LocalEndPoint.Port
314314
}
315315

316316
private IEnumerable<string> GetInterpreterArgs(TestCase test, string workingDir, string projectRootDir) {
317-
TestFrameworks.NodejsTestInfo testInfo = new TestFrameworks.NodejsTestInfo(test.FullyQualifiedName);
317+
TestFrameworks.NodejsTestInfo testInfo = new TestFrameworks.NodejsTestInfo(test.FullyQualifiedName, test.CodeFilePath);
318318
TestFrameworks.FrameworkDiscover discover = new TestFrameworks.FrameworkDiscover();
319319
return discover.Get(testInfo.TestFramework).ArgumentsToRunTests(testInfo.TestName, testInfo.ModulePath, workingDir, projectRootDir);
320320
}

Nodejs/Product/TestAdapter/TestFrameworks/NodejsTestInfo.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,70 @@
1515
//*********************************************************//
1616

1717
using System;
18+
using System.IO;
19+
using System.Security.Cryptography;
1820

1921
namespace Microsoft.NodejsTools.TestAdapter.TestFrameworks {
2022
class NodejsTestInfo {
21-
public NodejsTestInfo(string fullyQualifiedName) {
23+
public NodejsTestInfo(string fullyQualifiedName, string modulePath) {
2224
string[] parts = fullyQualifiedName.Split(new string[] { "::" }, StringSplitOptions.None);
2325
if (parts.Length != 3) {
2426
throw new ArgumentException("Invalid fully qualified test name");
2527
}
26-
ModulePath = parts[0];
28+
ModulePath = modulePath;
29+
ModuleName = parts[0];
2730
TestName = parts[1];
2831
TestFramework = parts[2];
2932
}
3033

3134
public NodejsTestInfo(string modulePath, string testName, string testFramework, int line, int column)
3235
{
3336
ModulePath = modulePath;
37+
SetModuleName(ModulePath);
3438
TestName = testName;
3539
TestFramework = testFramework;
3640
SourceLine = line;
3741
SourceColumn = column;
3842
}
3943

44+
private void SetModuleName(string modulePath)
45+
{
46+
ModuleName = String.Format("{0}[{1}]",
47+
(string)Path.GetFileName(modulePath).Split('.').GetValue(0),
48+
GetHash(modulePath));
49+
}
50+
51+
private string GetHash(string filePath)
52+
{
53+
try
54+
{
55+
using (FileStream stream = File.OpenRead(filePath))
56+
{
57+
SHA1Managed sha = new SHA1Managed();
58+
byte[] hash = sha.ComputeHash(stream);
59+
60+
// chop hash in half since we just need a unique ID
61+
Int32 startIndex = hash.Length / 2;
62+
sha.Dispose();
63+
64+
return BitConverter.ToString(hash, startIndex).Replace("-", String.Empty);
65+
}
66+
} catch (FileNotFoundException)
67+
{
68+
// Just return some default value and let node handle it later
69+
return "FILE_NOT_FOUND";
70+
}
71+
}
72+
4073
public string FullyQualifiedName {
4174
get {
42-
return ModulePath + "::" + TestName + "::" + TestFramework;
75+
return ModuleName + "::" + TestName + "::" + TestFramework;
4376
}
4477
}
4578
public string ModulePath { get; private set; }
4679

80+
public string ModuleName { get; private set; }
81+
4782
public string TestName { get; private set; }
4883

4984
public string TestFramework { get; private set; }

Nodejs/Tests/TestAdapterTests/NodejsTestInfoTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void ConstructFullyQualifiedName_ValidInput() {
1616
//Act
1717
NodejsTestInfo testInfo = new NodejsTestInfo(testFile, testName, testFramework, 0, 0);
1818
//Assert
19-
string expected = testFile + "::" + testName + "::" + testFramework;
19+
string expected = testInfo.ModuleName + "::" + testName + "::" + testFramework;
2020
Assert.AreEqual(expected, testInfo.FullyQualifiedName);
2121
Assert.AreEqual(testName, testInfo.TestName);
2222
Assert.AreEqual(testFramework, testInfo.TestFramework);
@@ -29,9 +29,10 @@ public void ConstructFullyQualifiedName_ValidInput() {
2929
public void ConstructFromQualifiedName_ThrowOnInValidInput() {
3030
//Arrange
3131
string badDummy = "c:\\dummy.js::dummy::dumm2::test1";
32+
string dummyPath = "c:\\dummyTest.js";
3233

3334
//Act
34-
NodejsTestInfo testInfo = new NodejsTestInfo(badDummy);
35+
NodejsTestInfo testInfo = new NodejsTestInfo(badDummy, dummyPath);
3536

3637
//Assert: N/A
3738
}

0 commit comments

Comments
 (0)