From 8e9cb40f393751e8676644e3ff16d448170d919a Mon Sep 17 00:00:00 2001 From: ozyx Date: Wed, 14 Jun 2017 14:25:35 -0700 Subject: [PATCH] Use hashed file contents as unique test ID --- Nodejs/Product/TestAdapter/TestExecutor.cs | 4 +- .../TestFrameworks/NodejsTestInfo.cs | 41 +++++++++++++++++-- .../TestAdapterTests/NodejsTestInfoTests.cs | 5 ++- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Nodejs/Product/TestAdapter/TestExecutor.cs b/Nodejs/Product/TestAdapter/TestExecutor.cs index 036949afd..8acd3e0d9 100644 --- a/Nodejs/Product/TestAdapter/TestExecutor.cs +++ b/Nodejs/Product/TestAdapter/TestExecutor.cs @@ -214,7 +214,7 @@ private void RunTestCases(IEnumerable 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) { @@ -314,7 +314,7 @@ select connection.LocalEndPoint.Port } private IEnumerable 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); } diff --git a/Nodejs/Product/TestAdapter/TestFrameworks/NodejsTestInfo.cs b/Nodejs/Product/TestAdapter/TestFrameworks/NodejsTestInfo.cs index 433f6f128..26628eba9 100644 --- a/Nodejs/Product/TestAdapter/TestFrameworks/NodejsTestInfo.cs +++ b/Nodejs/Product/TestAdapter/TestFrameworks/NodejsTestInfo.cs @@ -15,15 +15,18 @@ //*********************************************************// 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]; } @@ -31,19 +34,51 @@ public NodejsTestInfo(string fullyQualifiedName) { 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; } diff --git a/Nodejs/Tests/TestAdapterTests/NodejsTestInfoTests.cs b/Nodejs/Tests/TestAdapterTests/NodejsTestInfoTests.cs index e754ac8c6..241d8b83f 100644 --- a/Nodejs/Tests/TestAdapterTests/NodejsTestInfoTests.cs +++ b/Nodejs/Tests/TestAdapterTests/NodejsTestInfoTests.cs @@ -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); @@ -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 }