TsConfigProvider { List<String> tsconfigs(SensorContext context) throws IOException { for (Provider provider : providers) { List<String> tsconfigs = provider.tsconfigs(context); if (!tsconfigs.isEmpty()) { return tsconfigs; } } return emptyList(); } TsConfigProvider(TempFolder folder);   }
@Test public void should_lookup_tsconfig_files() throws Exception { Path tsconfig1 = baseDir.resolve("tsconfig.json"); Files.createFile(tsconfig1); Path subdir = baseDir.resolve("subdir"); Files.createDirectory(subdir); Files.createDirectory(subdir.resolve("node_modules")); Path tsconfig2 = Files.createFile(subdir.resolve("tsconfig.json")); Files.createFile(subdir.resolve("node_modules/tsconfig.json")); Files.createFile(subdir.resolve("base.tsconfig.json")); SensorContextTester ctx = SensorContextTester.create(baseDir); createInputFile(ctx, "file1.ts"); createInputFile(ctx, "file2.ts"); List<String> tsconfigs = new TsConfigProvider(tempFolder).tsconfigs(ctx); assertThat(tsconfigs).containsExactlyInAnyOrder(tsconfig1.toAbsolutePath().toString(), tsconfig2.toAbsolutePath().toString()); }
@Test public void should_use_tsconfig_from_property() throws Exception { Path baseDir = tempFolder.newDir().toPath(); Files.createFile(baseDir.resolve("custom.tsconfig.json")); SensorContextTester ctx = SensorContextTester.create(baseDir); ctx.setSettings(new MapSettings().setProperty("sonar.typescript.tsconfigPath", "custom.tsconfig.json")); createInputFile(ctx, "file.ts"); List<String> tsconfigs = new TsConfigProvider(tempFolder).tsconfigs(ctx); String absolutePath = baseDir.resolve("custom.tsconfig.json").toAbsolutePath().toString(); assertThat(tsconfigs).containsExactly(absolutePath); }
@Test public void should_validate_tsconfig_from_property() throws Exception { SensorContextTester ctx = SensorContextTester.create(baseDir); ctx.setSettings(new MapSettings().setProperty("sonar.typescript.tsconfigPath", "custom.tsconfig.json")); createInputFile(ctx, "file.ts"); String absolutePath = baseDir.resolve("custom.tsconfig.json").toAbsolutePath().toString(); assertThatThrownBy(() -> new TsConfigProvider(tempFolder).tsconfigs(ctx)) .isInstanceOf(IllegalStateException.class) .hasMessage("Provided tsconfig.json path doesn't exist. Path: '" + absolutePath + "'"); }
@Test public void should_use_absolute_path_from_property() throws Exception { Path baseDir = tempFolder.newDir().toPath(); Files.createFile(baseDir.resolve("custom.tsconfig.json")); SensorContextTester ctx = SensorContextTester.create(baseDir); String absolutePath = baseDir.resolve("custom.tsconfig.json").toAbsolutePath().toString(); ctx.setSettings(new MapSettings().setProperty("sonar.typescript.tsconfigPath", absolutePath)); createInputFile(ctx, "file.ts"); List<String> tsconfigs = new TsConfigProvider(tempFolder).tsconfigs(ctx); assertThat(tsconfigs).containsExactly(absolutePath); }
@Test public void should_create_tsconfig() throws Exception { SensorContextTester ctx = SensorContextTester.create(baseDir); createInputFile(ctx, "file1.ts"); createInputFile(ctx, "file2.ts"); List<String> tsconfigs = new TsConfigProvider(tempFolder).tsconfigs(ctx); assertThat(tsconfigs).hasSize(1); String tsconfig = new String(Files.readAllBytes(Paths.get(tsconfigs.get(0))), StandardCharsets.UTF_8); assertThat(tsconfig).isEqualTo("{\"files\":[\"moduleKey/file1.ts\",\"moduleKey/file2.ts\"],\"compilerOptions\":{}}"); }
@Test public void should_not_create_tsconfig_in_sonarlint() throws Exception { SensorContextTester ctx = SensorContextTester.create(baseDir); createInputFile(ctx, "file1.ts"); createInputFile(ctx, "file2.ts"); ctx.setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(4,4))); List<String> tsconfigs = new TsConfigProvider(tempFolder).tsconfigs(ctx); assertThat(tsconfigs).isEmpty(); }