Skip to content

Commit

Permalink
Merge pull request #1605 from ozyx/mocha-unique-id
Browse files Browse the repository at this point in the history
Use hashed file contents as unique test ID
  • Loading branch information
paulvanbrenk committed Jul 5, 2017
2 parents 1032c44 + 8e9cb40 commit aff39e9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Nodejs/Product/TestAdapter/TestExecutor.cs
Expand Up @@ -214,7 +214,7 @@ public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrame
}

// 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
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
Expand Up @@ -16,7 +16,7 @@ public class NodejsTestInfoTests {
//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 class NodejsTestInfoTests {
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

0 comments on commit aff39e9

Please sign in to comment.