Skip to content

Commit

Permalink
test(generator): allow loading module files from classpath
Browse files Browse the repository at this point in the history
This should ease testing for custom jhlite modules, that will be able to test against templates from main jhipster-lite modules
  • Loading branch information
murdos committed Feb 1, 2025
1 parent 19d63ba commit 9fc4a27
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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.net.URL;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
Expand Down Expand Up @@ -36,47 +37,47 @@ 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");
}

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");
return fileFromClasspath("generator/typescript/eslint.config.js.mustache", "eslint.config.js");
}

public static ModuleFile emptyLintStagedConfigFile() {
Expand All @@ -88,13 +89,22 @@ public static ModuleFile lintStagedConfigFileWithoutPrettier() {
}

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

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

public static ModuleFile fileFromClasspath(String source, String destination) {
URL sourceResource = JHipsterModulesAssertions.class.getClassLoader().getResource(source);
if (sourceResource == null) {
throw new AssertionError("Can't find file " + source + " in classpath");
}

return new ModuleFile(sourceResource.getPath(), destination);
}

public static JHipsterModuleAsserter assertThatModuleWithFiles(JHipsterModule module, ModuleFile... files) {
addFilesToProject(module.projectFolder(), files);

Expand Down
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 9fc4a27

Please sign in to comment.