Skip to content

Commit

Permalink
Remove useless exclusion in tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
qmonmert committed Oct 19, 2024
1 parent 1550be1 commit 82cc545
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@
import static tech.jhipster.lite.module.domain.JHipsterModule.from;
import static tech.jhipster.lite.module.domain.JHipsterModule.moduleBuilder;
import static tech.jhipster.lite.module.domain.JHipsterModule.packageName;
import static tech.jhipster.lite.module.domain.JHipsterModule.path;
import static tech.jhipster.lite.module.domain.JHipsterModule.scriptCommand;
import static tech.jhipster.lite.module.domain.JHipsterModule.scriptKey;
import static tech.jhipster.lite.module.domain.JHipsterModule.to;
import static tech.jhipster.lite.module.domain.npm.JHLiteNpmVersionSource.COMMON;

import java.util.function.Function;
import java.util.regex.Pattern;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.file.JHipsterDestination;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.module.domain.replacement.RegexReplacer;
import tech.jhipster.lite.shared.error.domain.Assert;

public class CypressModuleFactory {
Expand All @@ -30,31 +26,13 @@ public class CypressModuleFactory {
private static final String HOME = "home";
private static final String UTILS = "utils";

private static final String CYPRESS_COMPONENT_TESTS_EXCLUSION = "\"src/test/webapp/component/**/*.ts\"";
private static final String CYPRESS_E2E_TESTS_EXCLUSION = "\"src/test/webapp/e2e/**/*.ts\"";
private static final String EXCLUDE_KEY = "\"exclude\"";
private static final RegexReplacer NEW_EXCLUSION_REPLACER = new RegexReplacer(
(currentContent, replacement) -> !currentContent.contains(EXCLUDE_KEY),
Pattern.compile("\\n.*}\\s*$")
);

private static final Function<String, RegexReplacer> EXISTING_EXCLUSION_REPLACER_PROVIDER = (String exclusion) ->
new RegexReplacer(
(currentContent, replacement) -> currentContent.contains(EXCLUDE_KEY) && !currentContent.contains(exclusion),
Pattern.compile("(" + EXCLUDE_KEY + "\\s*:\\s*\\[[^]]+)]")
);

private static final Function<String, RegexReplacer> EMPTY_EXCLUSION_REPLACER_PROVIDER = (String exclusion) ->
new RegexReplacer(
(currentContent, replacement) -> currentContent.contains(EXCLUDE_KEY) && !currentContent.contains(exclusion),
Pattern.compile("(" + EXCLUDE_KEY + "\\s*:\\s*\\[\\s*)]")
);

public JHipsterModule buildComponentTestsModule(JHipsterModuleProperties properties) {
Assert.notNull("properties", properties);

//@formatter:off
return commonCypressModuleBuilder(properties, CYPRESS_COMPONENT_TESTS, CYPRESS_COMPONENT_TESTS_EXCLUSION)
return commonCypressModuleBuilder(properties, CYPRESS_COMPONENT_TESTS)
.packageJson()
.addDevDependency(packageName("start-server-and-test"), COMMON)
.addScript(scriptKey("test:component"), scriptCommand("start-server-and-test start http://localhost:9000 'cypress open --e2e --config-file src/test/webapp/component/cypress-config.ts'"))
Expand All @@ -74,7 +52,7 @@ public JHipsterModule buildE2ETestsModule(JHipsterModuleProperties properties) {
Assert.notNull("properties", properties);

//@formatter:off
return commonCypressModuleBuilder(properties, CYPRESS_E2E_TESTS, CYPRESS_E2E_TESTS_EXCLUSION)
return commonCypressModuleBuilder(properties, CYPRESS_E2E_TESTS)
.packageJson()
.addScript(scriptKey("e2e"), scriptCommand("cypress open --e2e --config-file src/test/webapp/e2e/cypress-config.ts"))
.addScript(scriptKey("e2e:headless"), scriptCommand("cypress run --headless --config-file src/test/webapp/e2e/cypress-config.ts"))
Expand All @@ -88,8 +66,7 @@ public JHipsterModule buildE2ETestsModule(JHipsterModuleProperties properties) {

private static JHipsterModuleBuilder commonCypressModuleBuilder(
JHipsterModuleProperties properties,
JHipsterDestination destinationFolder,
String tsconfigExclusion
JHipsterDestination destinationFolder
) {
//@formatter:off
return moduleBuilder(properties)
Expand All @@ -110,13 +87,6 @@ private static JHipsterModuleBuilder commonCypressModuleBuilder(
.addFile("DataSelector.ts")
.and()
.and()
.optionalReplacements()
.in(path("tsconfig.json"))
.add(EXISTING_EXCLUSION_REPLACER_PROVIDER.apply(tsconfigExclusion), "$1, "+tsconfigExclusion+"]")
.add(EMPTY_EXCLUSION_REPLACER_PROVIDER.apply(tsconfigExclusion), "$1"+tsconfigExclusion+"]")
.add(NEW_EXCLUSION_REPLACER, newExclusionNode(properties, tsconfigExclusion))
.and()
.and()
;
//@formatter:on
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,7 @@ void shouldBuildComponentTestsModuleOnProjectWithEmptyTsConfig() {
assertCypressComponentTestsModule(
packageJsonFile(),
file("src/test/resources/projects/empty-ts-config/tsconfig.json", "tsconfig.json")
)
.hasFile("tsconfig.json")
.containing(
"""
},
"exclude": ["src/test/webapp/component/**/*.ts"]
}\
"""
);
).hasFile("tsconfig.json");
}

@Test
Expand All @@ -46,7 +38,7 @@ void shouldBuildComponentTestsModuleOnProjectWithTsConfigWithEmptyExclusions() {
file("src/test/resources/projects/ts-config-with-empty-exclusions/tsconfig.json", "tsconfig.json")
)
.hasFile("tsconfig.json")
.containing("\"exclude\": [\"src/test/webapp/component/**/*.ts\"]");
.containing("\"exclude\": []");
}

@Test
Expand All @@ -56,9 +48,7 @@ void shouldBuildComponentTestsModuleOnProjectWithTsConfigWithExistingExclusions(
file("src/test/resources/projects/ts-config-with-exclusions/tsconfig.json", "tsconfig.json")
)
.hasFile("tsconfig.json")
.containing(
" \"exclude\": [\"src/test/webapp/integration/**/*spec.ts\", \"node_modules\", \"src/test/webapp/component/**/*.ts\"]"
);
.containing(" \"exclude\": [\"src/test/webapp/integration/**/*spec.ts\", \"node_modules\"]");
}

@Test
Expand Down Expand Up @@ -115,25 +105,18 @@ void shouldBuildE2eTestsModuleOnProjectWithoutTsConfig() {

@Test
void shouldBuildE2eTestsModuleOnProjectWithEmptyTsConfig() {
assertCypressE2ETestsModule(packageJsonFile(), file("src/test/resources/projects/empty-ts-config/tsconfig.json", "tsconfig.json"))
.hasFile("tsconfig.json")
.containing(
"""
},
"exclude": ["src/test/webapp/e2e/**/*.ts"]
}\
"""
);
assertCypressE2ETestsModule(
packageJsonFile(),
file("src/test/resources/projects/empty-ts-config/tsconfig.json", "tsconfig.json")
).hasFile("tsconfig.json");
}

@Test
void shouldBuildE2eTestsModuleOnProjectWithTsConfigWithEmptyExclusions() {
assertCypressE2ETestsModule(
packageJsonFile(),
file("src/test/resources/projects/ts-config-with-empty-exclusions/tsconfig.json", "tsconfig.json")
)
.hasFile("tsconfig.json")
.containing("\"exclude\": [\"src/test/webapp/e2e/**/*.ts\"]");
).hasFile("tsconfig.json");
}

@Test
Expand All @@ -143,7 +126,7 @@ void shouldBuildE2eTestsModuleOnProjectWithTsConfigWithExistingExclusions() {
file("src/test/resources/projects/ts-config-with-exclusions/tsconfig.json", "tsconfig.json")
)
.hasFile("tsconfig.json")
.containing(" \"exclude\": [\"src/test/webapp/integration/**/*spec.ts\", \"node_modules\", \"src/test/webapp/e2e/**/*.ts\"]");
.containing(" \"exclude\": [\"src/test/webapp/integration/**/*spec.ts\", \"node_modules\"]");
}

@Test
Expand All @@ -153,7 +136,7 @@ void shouldNotDuplicateCypressExclusion() {
file("src/test/resources/projects/ts-config-with-cypress-e2e-exclusions/tsconfig.json", "tsconfig.json")
)
.hasFile("tsconfig.json")
.containing(" \"exclude\": [\"src/test/webapp/e2e/**/*spec.ts\", \"node_modules\", \"src/test/webapp/e2e/**/*.ts\"]");
.containing(" \"exclude\": [\"src/test/webapp/e2e/**/*spec.ts\", \"node_modules\"]");
}

private static JHipsterModuleAsserter assertCypressE2ETestsModule(ModuleFile... files) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
}
},
"include": ["vite.config.ts", "src"],
"exclude": ["src/test/webapp/e2e/**/*spec.ts", "node_modules", "src/test/webapp/e2e/**/*.ts"]
"exclude": ["src/test/webapp/e2e/**/*spec.ts", "node_modules"]
}

0 comments on commit 82cc545

Please sign in to comment.