diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 0200254d6..b657e0215 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -2,16 +2,18 @@ name: PR on: pull_request jobs: pr: - runs-on: ubuntu-20.04 + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-20.04, windows-2019, macos-10.15, macos-11] steps: - uses: actions/checkout@v2 with: fetch-depth: 1 - - name: Set up JDK 11 - uses: actions/setup-java@v2 + - name: Set up JDK 8 and SBT + uses: olafurpg/setup-scala@v11 with: - distribution: 'adopt' - java-version: 8 + java-version: adopt@1.8 - uses: actions/cache@v2 with: path: | @@ -19,4 +21,5 @@ jobs: ~/.coursier key: ${{ runner.os }}-sbt-${{ hashfiles('**/build.sbt') }} - name: Compile and run tests - run: sbt clean test + run: sbt -v -Dfile.encoding=UTF-8 "clean; +test;" + shell: bash diff --git a/build.sbt b/build.sbt index b7f141947..a6ec9e5e3 100644 --- a/build.sbt +++ b/build.sbt @@ -4,8 +4,6 @@ val gitCommitString = SettingKey[String]("gitSha") enablePlugins(JavaAppPackaging, GitVersioning, BuildInfoPlugin) -Test / test := ((Test / test) dependsOn (cfgIntegrationTests / Test / test)).value - lazy val Fast = config("fast").extend(Test) configs(Fast) inConfig(Fast)(Defaults.testTasks) @@ -122,11 +120,5 @@ lazy val js2cpg = (project in file(".")).settings( buildInfoPackage := "io.shiftleft.js2cpg.core" ) -lazy val cfgIntegrationTests = project.settings( - commonSettings, - publish / skip := true, - Test / test := ((Test / test) dependsOn (js2cpg / stage)).value -) - Universal / packageName := name.value Universal / topLevelDirectory := None diff --git a/cfgIntegrationTests/src/test/scala/io/shiftleft/js2cpg/cpg/passes/IntegrationTestFixture.scala b/cfgIntegrationTests/src/test/scala/io/shiftleft/js2cpg/cpg/passes/IntegrationTestFixture.scala deleted file mode 100644 index cee18ca52..000000000 --- a/cfgIntegrationTests/src/test/scala/io/shiftleft/js2cpg/cpg/passes/IntegrationTestFixture.scala +++ /dev/null @@ -1,69 +0,0 @@ -package io.shiftleft.js2cpg.cpg.passes - -import better.files.File -import overflowdb.Config -import io.shiftleft.codepropertygraph.Cpg -import io.shiftleft.codepropertygraph.cpgloading.{CpgLoader, CpgLoaderConfig} - -import java.io -import scala.collection.mutable -import scala.sys.process.{Process, ProcessLogger} -import scala.util.{Failure, Success, Try} - -object IntegrationTestFixture { - - private val js2cpgPath = File(".") match { - case f if f.canonicalPath.endsWith("cfgIntegrationTests") => f.parent - case f => f - } - - private object ExternalCommand { - private val windowsSystemPrefix = "Windows" - private val osNameProperty = "os.name" - - def run(command: String): Try[String] = { - val result = mutable.ArrayBuffer.empty[String] - val lineHandler: String => Unit = line => { - result.addOne(line) - } - - val systemString = System.getProperty(osNameProperty) - val shellPrefix = - if (systemString != null && systemString.startsWith(windowsSystemPrefix)) { - "cmd" :: "/c" :: Nil - } else { - "sh" :: "-c" :: Nil - } - - Process(shellPrefix :+ command, new io.File(js2cpgPath.pathAsString)) - .!(ProcessLogger(lineHandler, lineHandler)) match { - case 0 => - Success(result.mkString(System.lineSeparator())) - case _ => - Failure(new RuntimeException(result.mkString(System.lineSeparator()))) - } - } - } -} - -abstract class IntegrationTestFixture { - - import IntegrationTestFixture.ExternalCommand - - protected def callFrontend(workspace: File, code: String): Try[Cpg] = { - val file = workspace / "test.js" - val cpgPath = workspace / "cpg.bin.zip" - file.write(code) - - ExternalCommand - .run( - s"./js2cpg.sh ${workspace.pathAsString} --output ${cpgPath.pathAsString} --no-ts --no-babel") - .map { _ => - CpgLoader - .loadFromOverflowDb( - CpgLoaderConfig.withDefaults.withOverflowConfig( - Config.withDefaults.withStorageLocation(cpgPath.pathAsString))) - } - } - -} diff --git a/src/main/scala/io/shiftleft/js2cpg/core/Config.scala b/src/main/scala/io/shiftleft/js2cpg/core/Config.scala index 9c3c2f83a..9e1a0afdf 100644 --- a/src/main/scala/io/shiftleft/js2cpg/core/Config.scala +++ b/src/main/scala/io/shiftleft/js2cpg/core/Config.scala @@ -74,7 +74,7 @@ case class Config(srcDir: String = "", val slIngoreFilePath = Paths.get(srcDir, Config.SL_IGNORE_FILE) val result = Using(FileUtils.bufferedSourceFromFile(slIngoreFilePath)) { bufferedSource => val content = FileUtils.contentFromBufferedSource(bufferedSource) - content.split("\n").toSeq.map(createPathForIgnore) + content.split(System.lineSeparator()).toSeq.map(createPathForIgnore) } result match { diff --git a/src/main/scala/io/shiftleft/js2cpg/io/EmScriptenCleaner.scala b/src/main/scala/io/shiftleft/js2cpg/io/EmScriptenCleaner.scala index 735054e75..dc7ce0651 100644 --- a/src/main/scala/io/shiftleft/js2cpg/io/EmScriptenCleaner.scala +++ b/src/main/scala/io/shiftleft/js2cpg/io/EmScriptenCleaner.scala @@ -14,7 +14,7 @@ object EmScriptenCleaner { val endIndex = lines.indexWhere(EMSCRIPTEN_END_FUNCS.matches) if (startIndex != -1 && endIndex != -1 && endIndex > startIndex) { (lines.slice(0, startIndex) ++ - Seq.fill(endIndex - startIndex - 1)("\n") ++ + Seq.fill(endIndex - startIndex - 1)(System.lineSeparator()) ++ lines.slice(endIndex + 1, lines.length)).iterator } else { lines.iterator diff --git a/src/main/scala/io/shiftleft/js2cpg/io/FileUtils.scala b/src/main/scala/io/shiftleft/js2cpg/io/FileUtils.scala index da0af5c73..2d227f48d 100644 --- a/src/main/scala/io/shiftleft/js2cpg/io/FileUtils.scala +++ b/src/main/scala/io/shiftleft/js2cpg/io/FileUtils.scala @@ -5,14 +5,14 @@ import better.files.File import java.io.Reader import java.math.BigInteger import java.nio.charset.{CharsetDecoder, CodingErrorAction} -import java.nio.file.{FileVisitResult, Files, Path, SimpleFileVisitor} +import java.nio.file.{Files, FileVisitResult, Path, SimpleFileVisitor} import io.shiftleft.js2cpg.core.Config import io.shiftleft.js2cpg.io.FileDefaults._ import org.slf4j.LoggerFactory import java.nio.file.attribute.BasicFileAttributes import scala.collection.concurrent.TrieMap -import scala.collection.{SortedMap, mutable} +import scala.collection.{mutable, SortedMap} import scala.io.{BufferedSource, Codec, Source} import scala.jdk.CollectionConverters._ @@ -33,7 +33,7 @@ object FileUtils { /** * Cleans the given path as String and removes unwanted elements that - * occure during transpilation on the Windows platform and/or CI environments. + * occur during transpilation on the Windows platform and/or CI environments. */ def cleanPath(sourceFileName: String): String = { val replaceDots = sourceFileName.replace("../", "") diff --git a/src/main/scala/io/shiftleft/js2cpg/parser/JavaScriptParser.scala b/src/main/scala/io/shiftleft/js2cpg/parser/JavaScriptParser.scala index fa4f87664..2a7e136a6 100644 --- a/src/main/scala/io/shiftleft/js2cpg/parser/JavaScriptParser.scala +++ b/src/main/scala/io/shiftleft/js2cpg/parser/JavaScriptParser.scala @@ -81,7 +81,7 @@ object JavaScriptParser { val lines = jsSource.source.getContent.toString.linesIterator.toSeq val replaceIndex = lines.lastIndexWhere(l => importRegex.matches(l.trim())) + 1 val (head, rest) = lines.splitAt(replaceIndex) - val fixedCode = (head ++ nodeJsFixWrapper(rest)).mkString("\n") + val fixedCode = (head ++ nodeJsFixWrapper(rest)).mkString(System.lineSeparator()) val source = Source.sourceFor(jsSource.filePath, fixedCode) source.toJsSource(jsSource.srcDir, jsSource.projectDir) } diff --git a/src/main/scala/io/shiftleft/js2cpg/parser/JsSource.scala b/src/main/scala/io/shiftleft/js2cpg/parser/JsSource.scala index 1d9e5bef9..24642e3b6 100644 --- a/src/main/scala/io/shiftleft/js2cpg/parser/JsSource.scala +++ b/src/main/scala/io/shiftleft/js2cpg/parser/JsSource.scala @@ -95,9 +95,12 @@ class JsSource(val srcDir: File, val projectDir: Path, val source: Source) { // on MacOS the path to the tmp dir is already there File("/") / replacedName } else { - // on all other OS we have to prepend it - if (srcDir.pathAsString.startsWith(tmpDir)) File(tmpDir) / replacedName - else File("/") / replacedName + // on all other OS we have to prepend it with special handling for Windows: + if (replacedName.contains("AppData/Local/Temp")) { + srcDir.root / "Users" / replacedName + } else { + File(tmpDir) / replacedName + } } srcFilePath } diff --git a/src/main/scala/io/shiftleft/js2cpg/preprocessing/EjsTranspiler.scala b/src/main/scala/io/shiftleft/js2cpg/preprocessing/EjsTranspiler.scala index 7341792d5..0b3bc694d 100644 --- a/src/main/scala/io/shiftleft/js2cpg/preprocessing/EjsTranspiler.scala +++ b/src/main/scala/io/shiftleft/js2cpg/preprocessing/EjsTranspiler.scala @@ -81,7 +81,7 @@ class EjsTranspiler(override val config: Config, override val projectPath: Path) result.append(extractedJsCode) } } - (result.mkString("\n"), sourceMap) + (result.mkString(System.lineSeparator()), sourceMap) } override def shouldRun(): Boolean = config.templateTranspiling && ejsFiles.nonEmpty diff --git a/cfgIntegrationTests/src/test/scala/io/shiftleft/js2cpg/cpg/passes/CfgIntegrationTest.scala b/src/test/scala/io/shiftleft/js2cpg/cpg/passes/CfgCreationPassTest.scala similarity index 99% rename from cfgIntegrationTests/src/test/scala/io/shiftleft/js2cpg/cpg/passes/CfgIntegrationTest.scala rename to src/test/scala/io/shiftleft/js2cpg/cpg/passes/CfgCreationPassTest.scala index 200d3cc4d..163d74e41 100644 --- a/cfgIntegrationTests/src/test/scala/io/shiftleft/js2cpg/cpg/passes/CfgIntegrationTest.scala +++ b/src/test/scala/io/shiftleft/js2cpg/cpg/passes/CfgCreationPassTest.scala @@ -3,24 +3,20 @@ package io.shiftleft.js2cpg.cpg.passes import better.files.File import io.shiftleft.codepropertygraph.Cpg import io.shiftleft.codepropertygraph.generated._ +import io.shiftleft.js2cpg.core.Report +import io.shiftleft.passes.IntervalKeyPool import io.shiftleft.semanticcpg.language._ import io.shiftleft.semanticcpg.passes.CfgCreationPass -import io.shiftleft.semanticcpg.passes.cfgcreation.Cfg.{ - AlwaysEdge, - CaseEdge, - CfgEdgeType, - FalseEdge, - TrueEdge -} +import io.shiftleft.semanticcpg.passes.cfgcreation.Cfg._ import overflowdb.traversal._ import overflowdb._ + import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec import scala.jdk.CollectionConverters._ -import scala.util.{Failure, Success} -class CfgIntegrationTest extends AnyWordSpec with Matchers { +class CfgCreationPassTest extends AnyWordSpec with Matchers { "CFG generation for simple fragments" should { "have correct structure for block expression" in { @@ -1290,17 +1286,17 @@ class CfgIntegrationTest extends AnyWordSpec with Matchers { } } - private class CfgFixture(code: String) extends IntegrationTestFixture { + private class CfgFixture(code: String) { - private var cpg: Cpg = Cpg.emptyCpg + private val cpg: Cpg = Cpg.emptyCpg File.usingTemporaryDirectory("js2cpgCfgIntegrationTest") { workspace => - cpg = callFrontend(workspace, code) match { - case Failure(exception) => fail(exception) - case Success(cpg) => - new CfgCreationPass(cpg).createAndApply() - cpg - } + val file = workspace / "test.js" + file.write(code) + val keyPool = new IntervalKeyPool(1001, 2000) + val filenames = List((file.path, file.parent.path)) + new AstCreationPass(workspace, filenames, cpg, keyPool, new Report()).createAndApply() + new CfgCreationPass(cpg).createAndApply() } private def matchCode(node: Node, code: String): Boolean = { diff --git a/src/test/scala/io/shiftleft/js2cpg/helpers/TestHelpers.scala b/src/test/scala/io/shiftleft/js2cpg/helpers/TestHelpers.scala deleted file mode 100644 index ff38938fb..000000000 --- a/src/test/scala/io/shiftleft/js2cpg/helpers/TestHelpers.scala +++ /dev/null @@ -1,30 +0,0 @@ -package io.shiftleft.js2cpg.helpers - -import com.oracle.js.parser.Source -import com.oracle.js.parser.ir.FunctionNode -import io.shiftleft.js2cpg.parser.{JavaScriptParser, JsSource} -import io.shiftleft.js2cpg.util.SourceWrapper._ - -import scala.util.Using - -// Put operations that are shared across different test cases here -object TestHelpers { - - private def parseInternal(src: JsSource): (JsSource, FunctionNode) = - JavaScriptParser.parseFromSource(src).swap - - def parse(code: String): (JsSource, FunctionNode) = { - val src = Source.sourceFor("test", code).toJsSource() - parseInternal(src) - } - - def parseFromUrl(fileUrl: String): (JsSource, FunctionNode) = { - Using.resource(scala.io.Source.fromFile(fileUrl)) { content => - val code = content.getLines().mkString("\n") - val src = Source.sourceFor(fileUrl, code).toJsSource() - parseInternal(src) - } - - } - -} diff --git a/src/test/scala/io/shiftleft/js2cpg/io/EmScriptenCleanerTest.scala b/src/test/scala/io/shiftleft/js2cpg/io/EmScriptenCleanerTest.scala index 2954fbded..b54193187 100644 --- a/src/test/scala/io/shiftleft/js2cpg/io/EmScriptenCleanerTest.scala +++ b/src/test/scala/io/shiftleft/js2cpg/io/EmScriptenCleanerTest.scala @@ -11,7 +11,7 @@ class EmScriptenCleanerTest extends AnyWordSpec with Matchers { """ |console.log("Hello"); |console.log("World!");""".stripMargin - val result = EmScriptenCleaner.clean(code.linesIterator).mkString("\n") + val result = EmScriptenCleaner.clean(code.linesIterator).mkString(System.lineSeparator()) result shouldBe code } @@ -21,7 +21,7 @@ class EmScriptenCleanerTest extends AnyWordSpec with Matchers { |// EMSCRIPTEN_START_FUNCS |console.log("Hello"); |console.log("World!");""".stripMargin - val result = EmScriptenCleaner.clean(code.linesIterator).mkString("\n") + val result = EmScriptenCleaner.clean(code.linesIterator).mkString(System.lineSeparator()) result shouldBe code } @@ -31,7 +31,7 @@ class EmScriptenCleanerTest extends AnyWordSpec with Matchers { |console.log("Hello"); |console.log("World!"); |// EMSCRIPTEN_END_FUNCS""".stripMargin - val result = EmScriptenCleaner.clean(code.linesIterator).mkString("\n") + val result = EmScriptenCleaner.clean(code.linesIterator).mkString(System.lineSeparator()) result shouldBe code } @@ -42,7 +42,7 @@ class EmScriptenCleanerTest extends AnyWordSpec with Matchers { |console.log("Hello"); |console.log("World!"); |// EMSCRIPTEN_START_FUNCS""".stripMargin - val result = EmScriptenCleaner.clean(code.linesIterator).mkString("\n") + val result = EmScriptenCleaner.clean(code.linesIterator).mkString(System.lineSeparator()) result shouldBe code } @@ -66,7 +66,7 @@ class EmScriptenCleanerTest extends AnyWordSpec with Matchers { | |otherCode(); |foobar();""".stripMargin - val result = EmScriptenCleaner.clean(code.linesIterator).mkString("\n") + val result = EmScriptenCleaner.clean(code.linesIterator).mkString(System.lineSeparator()) result shouldBe expected } } diff --git a/src/test/scala/io/shiftleft/js2cpg/io/ExcludeTest.scala b/src/test/scala/io/shiftleft/js2cpg/io/ExcludeTest.scala index 21bed7176..220293d21 100644 --- a/src/test/scala/io/shiftleft/js2cpg/io/ExcludeTest.scala +++ b/src/test/scala/io/shiftleft/js2cpg/io/ExcludeTest.scala @@ -3,14 +3,16 @@ package io.shiftleft.js2cpg.io import better.files.File import io.shiftleft.codepropertygraph.Cpg import io.shiftleft.codepropertygraph.cpgloading.{CpgLoader, CpgLoaderConfig} -import io.shiftleft.codepropertygraph.generated.{PropertyNames, NodeTypes} -import io.shiftleft.js2cpg.core.{Js2CpgMain, Js2cpgArgumentsParser} +import io.shiftleft.codepropertygraph.generated.{NodeTypes, PropertyNames} +import io.shiftleft.js2cpg.core.{Js2cpgArgumentsParser, Js2CpgMain} import org.scalatest.matchers.should.Matchers import org.scalatest.prop.TableDrivenPropertyChecks import org.scalatest.wordspec.AnyWordSpec import overflowdb._ import overflowdb.traversal.TraversalSource +import java.util.regex.Pattern + object ExcludeTest { private implicit class ToArg(val arg: String) extends AnyVal { def toArg: String = s"--$arg" @@ -22,7 +24,7 @@ class ExcludeTest extends AnyWordSpec with Matchers with TableDrivenPropertyChec import ExcludeTest._ import Js2cpgArgumentsParser._ - private val projectUnderTestPath: String = getClass.getResource("/excludes").getPath + private val projectUnderTestPath = File(getClass.getResource("/excludes").toURI).pathAsString private def fileNames(cpg: Cpg): List[String] = TraversalSource(cpg.graph).label(NodeTypes.FILE).property(PropertyNames.NAME).toList @@ -44,7 +46,8 @@ class ExcludeTest extends AnyWordSpec with Matchers with TableDrivenPropertyChec CpgLoaderConfig.withDefaults.withOverflowConfig( Config.withDefaults.withStorageLocation(cpgPath))) - fileNames(cpg) should contain theSameElementsAs expectedFiles + fileNames(cpg) should contain theSameElementsAs expectedFiles.map( + _.replace("/", java.io.File.separator)) } } @@ -90,7 +93,9 @@ class ExcludeTest extends AnyWordSpec with Matchers with TableDrivenPropertyChec Seq(EXCLUDE_REGEX.toArg, ".*(index|b)\\..*"), Set("a.js", "folder/c.js", "foo.bar/d.js")), (s"exclude a complete folder with ${EXCLUDE_REGEX.toArg}", - Seq(EXCLUDE_REGEX.toArg, ".*/folder/.*"), + Seq( + EXCLUDE_REGEX.toArg, + s".*${Pattern.quote(java.io.File.separator)}folder${Pattern.quote(java.io.File.separator)}.*"), Set("index.js", "a.js", "foo.bar/d.js")), // -- // Tests for mixed arguments diff --git a/src/test/scala/io/shiftleft/js2cpg/io/PrivateModulesTest.scala b/src/test/scala/io/shiftleft/js2cpg/io/PrivateModulesTest.scala index 980066994..72a93c56c 100644 --- a/src/test/scala/io/shiftleft/js2cpg/io/PrivateModulesTest.scala +++ b/src/test/scala/io/shiftleft/js2cpg/io/PrivateModulesTest.scala @@ -3,13 +3,15 @@ package io.shiftleft.js2cpg.io import better.files.File import io.shiftleft.codepropertygraph.Cpg import io.shiftleft.codepropertygraph.cpgloading.{CpgLoader, CpgLoaderConfig} -import io.shiftleft.codepropertygraph.generated.{PropertyNames, NodeTypes} +import io.shiftleft.codepropertygraph.generated.{NodeTypes, PropertyNames} import io.shiftleft.js2cpg.core.Js2CpgMain import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec import overflowdb._ import overflowdb.traversal.TraversalSource +import java.util.regex.Pattern + class PrivateModulesTest extends AnyWordSpec with Matchers { private def fileNames(cpg: Cpg): List[String] = { @@ -22,7 +24,7 @@ class PrivateModulesTest extends AnyWordSpec with Matchers { "Handling for private modules" should { "copy and generate js files correctly for a simple project" in { - val projectPath = getClass.getResource("/privatemodules").getPath + val projectPath = getClass.getResource("/privatemodules").toURI File.usingTemporaryDirectory() { tmpDir: File => val tmpProjectPath = File(projectPath).copyToDirectory(tmpDir) @@ -35,12 +37,13 @@ class PrivateModulesTest extends AnyWordSpec with Matchers { CpgLoaderConfig.withDefaults.withOverflowConfig( Config.withDefaults.withStorageLocation(cpgPath))) - fileNames(cpg) should contain allElementsOf Set("@privateA/a.js", "@privateB/b.js") + fileNames(cpg) should contain allElementsOf Set(s"@privateA${java.io.File.separator}a.js", + s"@privateB${java.io.File.separator}b.js") } } "copy and generate js files correctly for a simple project with additional private modules" in { - val projectPath = getClass.getResource("/privatemodules").getPath + val projectPath = getClass.getResource("/privatemodules").toURI File.usingTemporaryDirectory() { tmpDir: File => val tmpProjectPath = File(projectPath).copyToDirectory(tmpDir) @@ -59,15 +62,17 @@ class PrivateModulesTest extends AnyWordSpec with Matchers { CpgLoaderConfig.withDefaults.withOverflowConfig( Config.withDefaults.withStorageLocation(cpgPath))) - fileNames(cpg) should contain allElementsOf Set("@privateA/a.js", - "@privateB/b.js", - "@privateC/c.js", - "privateD/d.js") + fileNames(cpg) should contain allElementsOf Set( + s"@privateA${java.io.File.separator}a.js", + s"@privateB${java.io.File.separator}b.js", + s"@privateC${java.io.File.separator}c.js", + s"privateD${java.io.File.separator}d.js" + ) } } "copy and generate js files correctly for a simple project with filter" in { - val projectPath = getClass.getResource("/privatemodules").getPath + val projectPath = getClass.getResource("/privatemodules").toURI File.usingTemporaryDirectory() { tmpDir: File => val tmpProjectPath = File(projectPath).copyToDirectory(tmpDir) @@ -78,7 +83,7 @@ class PrivateModulesTest extends AnyWordSpec with Matchers { cpgPath, "--no-babel", "--exclude-regex", - ".*@privateA/a.js")) + s".*@privateA${Pattern.quote(java.io.File.separator)}a.js")) val cpg = CpgLoader @@ -86,12 +91,12 @@ class PrivateModulesTest extends AnyWordSpec with Matchers { CpgLoaderConfig.withDefaults.withOverflowConfig( Config.withDefaults.withStorageLocation(cpgPath))) - fileNames(cpg) should contain only "@privateB/b.js" + fileNames(cpg) should contain only s"@privateB${java.io.File.separator}b.js" } } "copy and generate js files correctly for a simple project with no private module being references" in { - val projectPath = getClass.getResource("/ignoreprivatemodules").getPath + val projectPath = getClass.getResource("/ignoreprivatemodules").toURI File.usingTemporaryDirectory() { tmpDir: File => val tmpProjectPath = File(projectPath).copyToDirectory(tmpDir) diff --git a/src/test/scala/io/shiftleft/js2cpg/preprocessing/TranspilationRunnerTest.scala b/src/test/scala/io/shiftleft/js2cpg/preprocessing/TranspilationRunnerTest.scala index 35a6c3a90..79e419300 100644 --- a/src/test/scala/io/shiftleft/js2cpg/preprocessing/TranspilationRunnerTest.scala +++ b/src/test/scala/io/shiftleft/js2cpg/preprocessing/TranspilationRunnerTest.scala @@ -34,7 +34,7 @@ class TranspilationRunnerTest extends AnyWordSpec with Matchers { "TranspilationRunner" should { "generate js files correctly for a simple Babel project" in { - val projectPath = getClass.getResource("/babel").getPath + val projectPath = getClass.getResource("/babel").toURI File.usingTemporaryDirectory() { tmpDir: File => File.usingTemporaryDirectory() { transpileOutDir: File => val tmpProjectPath = File(projectPath).copyToDirectory(tmpDir) @@ -55,7 +55,7 @@ class TranspilationRunnerTest extends AnyWordSpec with Matchers { } "generate and use sourcemap files correctly" in { - val projectPath = getClass.getResource("/typescript").getPath + val projectPath = getClass.getResource("/typescript").toURI File.usingTemporaryDirectory() { tmpDir: File => val tmpProjectPath = File(projectPath).copyToDirectory(tmpDir) @@ -73,7 +73,7 @@ class TranspilationRunnerTest extends AnyWordSpec with Matchers { } "generate js files correctly for a simple Typescript project" in { - val projectPath = getClass.getResource("/typescript").getPath + val projectPath = getClass.getResource("/typescript").toURI File.usingTemporaryDirectory() { tmpDir: File => File.usingTemporaryDirectory() { transpileOutDir: File => val tmpProjectPath = File(projectPath).copyToDirectory(tmpDir) @@ -106,7 +106,7 @@ class TranspilationRunnerTest extends AnyWordSpec with Matchers { jsFilesAfterTranspilation.map( f => File(f._1).contentAsString - .split("\n") + .split(System.lineSeparator()) .head // we ignore the sourcemap reference comment here .mkString .stripLineEnd)) shouldBe "console.log(\"Hello World!\");" @@ -115,7 +115,7 @@ class TranspilationRunnerTest extends AnyWordSpec with Matchers { } "generate js files correctly for a simple Typescript project including test files" in { - val projectPath = getClass.getResource("/typescript").getPath + val projectPath = getClass.getResource("/typescript").toURI File.usingTemporaryDirectory() { tmpDir: File => val tmpProjectPath = File(projectPath).copyToDirectory(tmpDir) @@ -130,13 +130,13 @@ class TranspilationRunnerTest extends AnyWordSpec with Matchers { Config.withDefaults.withStorageLocation(cpgPath))) fileNames(cpg) should contain allElementsOf Set("a.ts", "b.ts", - "tests/a.test.ts", - "tests/b.spec.ts") + s"tests${java.io.File.separator}a.test.ts", + s"tests${java.io.File.separator}b.spec.ts") } } "generate js files correctly for a simple Vue.js project" in { - val projectPath = getClass.getResource("/vue").getPath + val projectPath = getClass.getResource("/vue").toURI File.usingTemporaryDirectory() { tmpDir: File => val tmpProjectPath = File(projectPath).copyToDirectory(tmpDir) @@ -148,12 +148,13 @@ class TranspilationRunnerTest extends AnyWordSpec with Matchers { .loadFromOverflowDb( CpgLoaderConfig.withDefaults.withOverflowConfig( Config.withDefaults.withStorageLocation(cpgPath))) - fileNames(cpg) should contain allElementsOf Set("src/main.js", "src/App.vue") + fileNames(cpg) should contain allElementsOf Set(s"src${java.io.File.separator}main.js", + s"src${java.io.File.separator}App.vue") } } "generate js file correctly for a EJS template file" in { - val projectPath = getClass.getResource("/ejs").getPath + val projectPath = getClass.getResource("/ejs").toURI File.usingTemporaryDirectory() { tmpDir: File => val tmpProjectPath = File(projectPath).copyToDirectory(tmpDir) @@ -172,7 +173,7 @@ class TranspilationRunnerTest extends AnyWordSpec with Matchers { } "generate js file correctly for a pug template file" in { - val projectPath = getClass.getResource("/pug").getPath + val projectPath = getClass.getResource("/pug").toURI File.usingTemporaryDirectory() { tmpDir: File => val tmpProjectPath = File(projectPath).copyToDirectory(tmpDir)