Skip to content

Commit

Permalink
Merge pull request jhipster#11836 from murdos/test-load-module-files-…
Browse files Browse the repository at this point in the history
…from-classpath

test(generator): allow loading module files from classpath
  • Loading branch information
murdos authored Feb 6, 2025
2 parents 041bfdd + 8af39f8 commit c3f0b9d
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 53 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Dockerfile
mvnw
gradlew
.husky/_/
src/test/resources/**/*.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.mockito.Mockito.verify;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.assertThatModuleWithFiles;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.lintStagedConfigFile;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.lintStagedConfigFileWithPrettier;
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;
Expand Down Expand Up @@ -37,7 +37,7 @@ void shouldCreateAngularModule() {

JHipsterModule module = factory.buildModule(properties);

assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFile())
assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFileWithPrettier())
.hasFile("package.json")
.containing(nodeDependency("zone.js"))
.containing(nodeDependency("tslib"))
Expand Down Expand Up @@ -130,7 +130,7 @@ void shouldProxyBeUpdatedWhenServerPortPropertyNotDefault() {
.build();

JHipsterModule module = factory.buildModule(properties);
assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFile())
assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFileWithPrettier())
.hasFile("proxy.conf.json")
.containing("\"target\": \"http://localhost:8081\"")
.notContaining("\"target\": \"http://localhost:8080\"");
Expand All @@ -143,7 +143,7 @@ void shouldProxyBeDefaultWhenServerPortPropertyMissing() {
.build();

JHipsterModule module = factory.buildModule(properties);
assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFile())
assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFileWithPrettier())
.hasFile("proxy.conf.json")
.containing("\"target\": \"http://localhost:8080\"");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.mockito.Mockito.verify;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.assertThatModuleWithFiles;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.eslintConfigFile;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.lintStagedConfigFile;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.lintStagedConfigFileWithPrettier;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.nodeDependency;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.packageJsonFile;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.tsConfigFile;
Expand Down Expand Up @@ -38,7 +38,14 @@ void shouldBuildModuleWithStyle() {
.build();
JHipsterModule module = factory.buildModule(properties);

assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFile(), eslintConfigFile(), tsConfigFile(), vitestConfigFile())
assertThatModuleWithFiles(
module,
packageJsonFile(),
lintStagedConfigFileWithPrettier(),
eslintConfigFile(),
tsConfigFile(),
vitestConfigFile()
)
.hasFile("package.json")
.notContaining(nodeDependency("@tsconfig/recommended"))
.containing(nodeDependency("@testing-library/dom"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.mockito.Mockito.verify;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.assertThatModuleWithFiles;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.lintStagedConfigFile;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.lintStagedConfigFileWithPrettier;
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;
Expand Down Expand Up @@ -36,7 +36,7 @@ void shouldCreateSvelteModule() {
JHipsterModule module = factory.buildSvelteModule(properties);

// @formatter:off
assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFile())
assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFileWithPrettier())
.hasFile(".gitignore")
.containing("""
# Svelte
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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.eslintConfigFile;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.lintStagedConfigFile;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.lintStagedConfigFileWithPrettier;
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;
Expand Down Expand Up @@ -43,7 +43,7 @@ void shouldCreateVueModule() {
JHipsterModule module = factory.buildVueModule(properties);

//@formatter:off
assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFile(), tsConfigFile(), vitestConfigFile(), eslintConfigFile())
assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFileWithPrettier(), tsConfigFile(), vitestConfigFile(), eslintConfigFile())
.hasFiles("documentation/vue.md")
.hasFile("package.json")
.notContaining(nodeDependency("@tsconfig/recommended"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package tech.jhipster.lite.generator.prettier.domain;

import static org.mockito.Mockito.verify;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.assertThatModuleWithFiles;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.emptyLintStagedConfigFile;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.lintStagedConfigFileWithoutPrettier;
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 static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.*;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -19,6 +14,7 @@
import tech.jhipster.lite.module.domain.JHipsterModulesFixture;
import tech.jhipster.lite.module.domain.npm.NpmLazyInstaller;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions;

@UnitTest
@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -78,7 +74,7 @@ void shouldBuildModuleWithEmptyLintStaged() {

JHipsterModule module = factory.buildModule(properties);

assertThatModuleWithFiles(module, packageJsonFile(), emptyLintStagedConfigFile())
assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFile())
.hasFile(".lintstagedrc.cjs")
.containing(
"""
Expand All @@ -97,4 +93,8 @@ private JHipsterModuleProperties properties(String folder) {
.put("endOfLine", "crlf")
.build();
}

public static JHipsterModulesAssertions.ModuleFile lintStagedConfigFileWithoutPrettier() {
return file("src/test/resources/projects/init/.lintstagedrc.withoutPrettier.cjs", ".lintstagedrc.cjs");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void shouldBuildModuleForMaven() {

JHipsterModule module = factory.buildProtobufBackwardsCompatibilityCheckModule(properties);

assertThatModuleWithFiles(module, pomFile(), lintStagedConfigFile())
assertThatModuleWithFiles(module, pomFile(), lintStagedConfigFileWithPrettier())
.hasFile(".lintstagedrc.cjs")
.containing(
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tech.jhipster.lite.module.infrastructure.secondary;

import static org.assertj.core.api.Assertions.*;
import static tech.jhipster.lite.TestFileUtils.*;
import static org.assertj.core.api.Assertions.assertThat;
import static tech.jhipster.lite.TestFileUtils.contentNormalizingNewLines;

import java.io.IOException;
import java.nio.file.FileVisitResult;
Expand All @@ -20,6 +20,9 @@
import org.approvaltests.scrubbers.NoOpScrubber;
import org.approvaltests.scrubbers.RegExScrubber;
import org.assertj.core.api.SoftAssertions;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamSource;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.JHipsterModuleUpgrade;
import tech.jhipster.lite.module.domain.properties.JHipsterProjectFolder;
Expand All @@ -36,63 +39,63 @@ public static JHipsterModuleAsserter assertThatModule(JHipsterModule module) {
}

public static ModuleFile pomFile() {
return file("src/test/resources/projects/init-maven/pom.xml", "pom.xml");
return fileFromClasspath("generator/buildtool/maven/pom.xml.mustache", "pom.xml");
}

public static ModuleFile gradleBuildFile() {
return file("src/test/resources/projects/init-gradle/build.gradle.kts", "build.gradle.kts");
return fileFromClasspath("generator/buildtool/gradle/build.gradle.kts.mustache", "build.gradle.kts");
}

public static ModuleFile gradleSettingsFile() {
return file("src/test/resources/projects/init-gradle/settings.gradle.kts", "settings.gradle.kts");
return fileFromClasspath("generator/buildtool/gradle/settings.gradle.kts.mustache", "settings.gradle.kts");
}

public static ModuleFile gradleLibsVersionFile() {
return file("src/test/resources/projects/init-gradle/gradle/libs.versions.toml", "gradle/libs.versions.toml");
return fileFromClasspath("generator/buildtool/gradle/gradle/libs.versions.toml", "gradle/libs.versions.toml");
}

public static ModuleFile logbackFile() {
return file("src/test/resources/projects/logback/logback.xml", "src/main/resources/logback-spring.xml");
return fileFromClasspath("generator/server/springboot/core/test/logback.xml.mustache", "src/main/resources/logback-spring.xml");
}

public static ModuleFile testLogbackFile() {
return file("src/test/resources/projects/logback/logback.xml", "src/test/resources/logback.xml");
return fileFromClasspath("generator/server/springboot/core/test/logback.xml.mustache", "src/test/resources/logback.xml");
}

public static ModuleFile packageJsonFile() {
return file("src/test/resources/projects/empty-node/package.json", "package.json");
return fileFromClasspath("generator/init/package.json.mustache", "package.json");
}

public static ModuleFile lintStagedConfigFile() {
return file("src/test/resources/projects/init/.lintstagedrc.cjs", ".lintstagedrc.cjs");
return fileFromClasspath("generator/init/.lintstagedrc.cjs", ".lintstagedrc.cjs");
}

public static ModuleFile lintStagedConfigFileWithPrettier() {
return fileFromClasspath("projects/init/.lintstagedrc.withPrettierModuleApplied.cjs", ".lintstagedrc.cjs");
}

public static ModuleFile tsConfigFile() {
return file("src/main/resources/generator/typescript/tsconfig.json", "tsconfig.json");
return fileFromClasspath("generator/typescript/tsconfig.json", "tsconfig.json");
}

public static ModuleFile vitestConfigFile() {
return file("src/main/resources/generator/typescript/vitest.config.ts.mustache", "vitest.config.ts");
return fileFromClasspath("generator/typescript/vitest.config.ts.mustache", "vitest.config.ts");
}

public static ModuleFile eslintConfigFile() {
return file("src/main/resources/generator/typescript/eslint.config.js.mustache", "eslint.config.js");
}

public static ModuleFile emptyLintStagedConfigFile() {
return file("src/test/resources/projects/init/.lintstagedrc.empty.cjs", ".lintstagedrc.cjs");
return fileFromClasspath("generator/typescript/eslint.config.js.mustache", "eslint.config.js");
}

public static ModuleFile lintStagedConfigFileWithoutPrettier() {
return file("src/test/resources/projects/init/.lintstagedrc.withoutPrettier.cjs", ".lintstagedrc.cjs");
public static ModuleFile readmeFile() {
return fileFromClasspath("generator/init/README.md.mustache", "README.md");
}

public static ModuleFile readmeFile() {
return file("src/test/resources/projects/README.md", "README.md");
public static ModuleFile file(String sourcePath, String destination) {
return new ModuleFile(new FileSystemResource(sourcePath), destination);
}

public static ModuleFile file(String source, String destination) {
return new ModuleFile(source, destination);
public static ModuleFile fileFromClasspath(String source, String destination) {
return new ModuleFile(new ClassPathResource(source), destination);
}

public static JHipsterModuleAsserter assertThatModuleWithFiles(JHipsterModule module, ModuleFile... files) {
Expand Down Expand Up @@ -146,7 +149,7 @@ private static void addFilesToProject(JHipsterProjectFolder project, ModuleFile.
}

try {
Files.copy(Path.of(file.source), destination);
Files.copy(file.source.getInputStream(), destination);
} catch (IOException e) {
throw new AssertionError(e);
}
Expand Down Expand Up @@ -444,9 +447,13 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
}
}

public record ModuleFile(String source, String destination) {
public record ModuleFile(InputStreamSource source, String destination) {
public ModuleFile(String sourcePath, String destination) {
this(new FileSystemResource(sourcePath), destination);
}

public ModuleFile {
Assert.notBlank("source", source);
Assert.notNull("source", source);
Assert.notBlank("destination", destination);
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/test/resources/projects/init/.lintstagedrc.empty.cjs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ java {
}
}


jacoco {
toolVersion = libs.versions.jacoco.get()
}
Expand All @@ -43,7 +42,7 @@ repositories {
// jhipster-needle-gradle-repositories
}

group = "tech.jhipster.jhlitest"
group = "com.test.myapp"
version = "0.0.1-SNAPSHOT"

val profiles = (project.findProperty("profiles") as String? ?: "")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "test-jhipster-project",
"name": "{{ dasherizedBaseName }}",
"version": "[version]",
"private": true,
"description": "JHipster Project",
Expand All @@ -10,7 +10,7 @@
},

"engines": {
"node": ">=16.13.0"
"node": ">={{ nodeMajorVersion }}"
},
"scripts": {
"build": "ng build --output-path=build/classes/static",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "test-jhipster-project",
"name": "{{ dasherizedBaseName }}",
"version": "[version]",
"private": true,
"description": "JHipster Project",
Expand All @@ -10,7 +10,7 @@
},

"engines": {
"node": ">=16.13.0"
"node": ">={{ nodeMajorVersion }}"
},
"scripts": {
"build": "ng build --output-path=target/classes/static",
Expand Down

0 comments on commit c3f0b9d

Please sign in to comment.