Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove useless exclusion in tsconfig #11161

Merged
merged 2 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package tech.jhipster.lite.generator.client.tools.cypress.domain;

import static tech.jhipster.lite.module.domain.JHipsterModule.JHipsterModuleBuilder;
import static tech.jhipster.lite.module.domain.JHipsterModule.LINE_BREAK;
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 +25,11 @@ 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 +49,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 +63,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,25 +84,7 @@ 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
}

private static String newExclusionNode(JHipsterModuleProperties properties, String exclusion) {
return new StringBuilder()
.append(",")
.append(LINE_BREAK)
.append(properties.indentation().spaces())
.append("\"exclude\": [" + exclusion + "]")
.append(LINE_BREAK)
.append("}")
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package tech.jhipster.lite.generator.client.tools.cypress.domain;

import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.*;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.ModuleFile;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.assertThatModuleWithFiles;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.nodeDependency;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.nodeScript;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.packageJsonFile;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -19,64 +23,13 @@ class CypressModuleFactoryTest {
class ComponentTests {

@Test
void shouldBuildComponentTestsModuleOnProjectWithoutTsConfig() {
assertCypressComponentTestsModule(packageJsonFile());
}

@Test
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"]
}\
"""
);
}

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

@Test
void shouldBuildComponentTestsModuleOnProjectWithTsConfigWithExistingExclusions() {
assertCypressComponentTestsModule(
packageJsonFile(),
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\"]"
);
}

@Test
void shouldNotDuplicateCypressExclusion() {
assertCypressComponentTestsModule(
packageJsonFile(),
file("src/test/resources/projects/ts-config-with-cypress-component-exclusions/tsconfig.json", "tsconfig.json")
)
.hasFile("tsconfig.json")
.containing(" \"exclude\": [\"src/test/webapp/component/**/*spec.ts\", \"node_modules\", \"src/test/webapp/component/**/*.ts\"]");
}

private static JHipsterModuleAsserter assertCypressComponentTestsModule(ModuleFile... files) {
void shouldBuildComponentTestsModule() {
ModuleFile[] files = new ModuleFile[] { packageJsonFile() };
JHipsterModuleProperties properties = JHipsterModulesFixture.propertiesBuilder(TestFileUtils.tmpDirForTest()).build();

JHipsterModule module = factory.buildComponentTestsModule(properties);

return assertThatModuleWithFiles(module, files)
assertThatModuleWithFiles(module, files)
.hasFile("package.json")
.containing(nodeDependency("cypress"))
.containing(nodeDependency("eslint-plugin-cypress"))
Expand Down Expand Up @@ -109,59 +62,13 @@ private static JHipsterModuleAsserter assertCypressComponentTestsModule(ModuleFi
class E2ETests {

@Test
void shouldBuildE2eTestsModuleOnProjectWithoutTsConfig() {
assertCypressE2ETestsModule(packageJsonFile());
}

@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"]
}\
"""
);
}

@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\"]");
}

@Test
void shouldBuildE2eTestsModuleOnProjectWithTsConfigWithExistingExclusions() {
assertCypressE2ETestsModule(
packageJsonFile(),
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\"]");
}

@Test
void shouldNotDuplicateCypressExclusion() {
assertCypressE2ETestsModule(
packageJsonFile(),
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\"]");
}

private static JHipsterModuleAsserter assertCypressE2ETestsModule(ModuleFile... files) {
void shouldBuildE2eTestsModule() {
ModuleFile[] files = new ModuleFile[] { packageJsonFile() };
JHipsterModuleProperties properties = JHipsterModulesFixture.propertiesBuilder(TestFileUtils.tmpDirForTest()).build();

JHipsterModule module = factory.buildE2ETestsModule(properties);

return assertThatModuleWithFiles(module, files)
assertThatModuleWithFiles(module, files)
.hasFile("package.json")
.containing(nodeDependency("cypress"))
.containing(nodeDependency("eslint-plugin-cypress"))
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading