-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
815d9cb
commit 3128c00
Showing
13 changed files
with
468 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/test/java/net/neoforged/moddevgradle/functional/AbstractFunctionalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package net.neoforged.moddevgradle.functional; | ||
|
||
import org.intellij.lang.annotations.Language; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
abstract class AbstractFunctionalTest { | ||
static final String DEFAULT_NEOFORGE_VERSION = "21.0.133-beta"; | ||
|
||
static final Map<String, String> DEFAULT_PLACEHOLDERS = Map.of( | ||
"DEFAULT_NEOFORGE_VERSION", DEFAULT_NEOFORGE_VERSION | ||
); | ||
|
||
@TempDir | ||
File testProjectDir; | ||
File settingsFile; | ||
File buildFile; | ||
|
||
@BeforeEach | ||
final void setBaseFiles() { | ||
settingsFile = new File(testProjectDir, "settings.gradle"); | ||
buildFile = new File(testProjectDir, "build.gradle"); | ||
} | ||
|
||
protected final void writeFile(File destination, String content) throws IOException { | ||
Files.writeString(destination.toPath(), content); | ||
} | ||
|
||
protected final void writeProjectFile(String relativePath, String content) throws IOException { | ||
var destination = testProjectDir.toPath().resolve(relativePath); | ||
Files.createDirectories(destination.getParent()); | ||
Files.writeString(destination, content); | ||
} | ||
|
||
void writeGroovySettingsScript(@Language("gradle") String text, Object... args) throws IOException { | ||
writeFile(settingsFile, interpolateTemplate(text, args)); | ||
} | ||
|
||
void writeGroovyBuildScript(@Language("gradle") String text, Object... args) throws IOException { | ||
writeFile(buildFile, interpolateTemplate(text, args)); | ||
} | ||
|
||
private static String interpolateTemplate(@Language("gradle") String text, Object[] args) { | ||
var m = Pattern.compile("\\{(\\d+|[A-Z_]+)}"); | ||
var body = m.matcher(text).replaceAll(matchResult -> { | ||
Object arg; | ||
var key = matchResult.group(1); | ||
try { | ||
int index = Integer.parseUnsignedInt(key); | ||
arg = args[index]; | ||
} catch (NumberFormatException ignored) { | ||
arg = DEFAULT_PLACEHOLDERS.get(key); | ||
if (arg == null) { | ||
throw new IllegalArgumentException("Invalid placeholder: " + key); | ||
} | ||
} | ||
if (arg instanceof Path path) { | ||
arg = path.toAbsolutePath().toString().replace('\\', '/'); | ||
} else if (arg instanceof File file) { | ||
arg = file.getAbsolutePath().replace('\\', '/'); | ||
} | ||
return Matcher.quoteReplacement(arg.toString()); | ||
}); | ||
return body; | ||
} | ||
|
||
protected final void clearProjectDir() { | ||
clearContent(testProjectDir); | ||
} | ||
|
||
private void clearContent(File file) { | ||
var content = file.listFiles(); | ||
if (content != null) { | ||
for (var child : content) { | ||
if (child.isDirectory()) { | ||
clearContent(child); | ||
} | ||
child.delete(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.