Skip to content

Commit

Permalink
Merge pull request #91 from mvnpm/add-scss-tests
Browse files Browse the repository at this point in the history
Add scss tests
  • Loading branch information
edewit authored Feb 1, 2024
2 parents 676113f + b876ed8 commit e405e68
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public BundleOptionsBuilder addEntryPoint(Path rootDir, String script) {
return this;
}

public BundleOptionsBuilder addEntryPoint(String script) {
if (options.getWorkDir() == null) {
throw new IllegalArgumentException("Workdir must be set");
}
addEntryPoint(new FileEntryPoint(options.getWorkDir(), script));
return this;
}

protected BundleOptionsBuilder addEntryPoint(EntryPoint entry) {
if (options.getEntries() == null) {
options.setEntries(new ArrayList<>());
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/io/mvnpm/esbuild/resolve/BaseResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;

public abstract class BaseResolver {
private static final String UNIX_PATH = "package/bin/esbuild";
private static final String WINDOWS_EXE_PATH = "package/esbuild.exe";
private static final String PATH = "package/bin/esbuild";

public static final String EXECUTABLE_PATH = resolveExecutablePath();
private static final String WINDOWS_EXE_PATH = "package/esbuild.exe";

public static final String CLASSIFIER = determineClassifier();

Expand All @@ -33,8 +32,12 @@ public BaseResolver(Resolver resolver) {
this.resolver = requireNonNull(resolver, "resolver is required");
}

private static String resolveExecutablePath() {
return isWindows() ? WINDOWS_EXE_PATH : UNIX_PATH;
public static String resolveBundledExecutablePath() {
return isWindows() ? PATH + ".exe" : PATH;
}

public static String resolveExecutablePath() {
return isWindows() ? WINDOWS_EXE_PATH : PATH;
}

private static boolean isWindows() {
Expand Down Expand Up @@ -119,7 +122,7 @@ static Path extract(InputStream archive, File destination) throws IOException {
}
}

return destination.toPath().resolve(resolveExecutablePath());
return destination.toPath();
}

static Path createDestination(String version) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ public BundleCacheResolver(Resolver resolver) {

@Override
public Path resolve(String version) throws IOException {
final Path path = super.resolve(ESBUILD_EMBEDDED_VERSION);
if (Files.isExecutable(path)) {
return path;
final Path path = getLocation(ESBUILD_EMBEDDED_VERSION);
final Path executablePath = path.resolve(resolveBundledExecutablePath());
if (Files.isExecutable(executablePath)) {
return executablePath;
}
return resolver.resolve(version);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public Path resolve(String version) throws IOException {
final InputStream resource = getClass().getResourceAsStream("/esbuild-%s-%s.tgz".formatted(CLASSIFIER, version));

if (resource != null) {
return extract(resource, ESBUILD_EMBEDDED_VERSION);
return extract(resource, ESBUILD_EMBEDDED_VERSION).resolve(resolveBundledExecutablePath());
}

return resolver.resolve(version);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/mvnpm/esbuild/resolve/CacheResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public CacheResolver(Resolver resolver) {
@Override
public Path resolve(String version) throws IOException {
final Path path = getLocation(version);
final Path executable = path.resolve(EXECUTABLE_PATH);
final Path executable = path.resolve(resolveExecutablePath());
if (Files.isExecutable(executable)) {
return executable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Path resolve(String version) throws IOException {

try {
downloadFile(new URL(url), tarFile);
return extract(Files.newInputStream(tarFile), destination.toFile());
return extract(Files.newInputStream(tarFile), destination.toFile()).resolve(resolveExecutablePath());
} catch (IOException e) {
return resolver.resolve(version);
}
Expand Down
67 changes: 51 additions & 16 deletions src/test/java/io/mvnpm/esbuild/BundlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -16,40 +16,61 @@
import io.mvnpm.esbuild.model.BundleOptions;
import io.mvnpm.esbuild.model.BundleOptionsBuilder;
import io.mvnpm.esbuild.model.BundleResult;
import io.mvnpm.esbuild.model.EsBuildConfigBuilder;
import io.mvnpm.esbuild.model.WebDependency.WebDependencyType;

public class BundlerTest {

@Test
public void shouldBundleMvnpm() throws URISyntaxException, IOException {
executeTest("/mvnpm/stimulus-3.2.1.jar", WebDependencyType.MVNPM, "application-mvnpm.js", true);
executeTest(List.of("/mvnpm/stimulus-3.2.1.jar"), WebDependencyType.MVNPM, "application-mvnpm.js", true);
}

@Test
public void shouldBundleMvnpmSources() throws URISyntaxException, IOException {
executeTest("/mvnpm/moment-2.29.4-sources.jar", WebDependencyType.MVNPM, "application-mvnpm.ts", true);
executeTest(List.of("/mvnpm/moment-2.29.4-sources.jar"), WebDependencyType.MVNPM,
"application-mvnpm.ts", true);
}

@Test
void shouldBundleWithScss() throws IOException, URISyntaxException {
final Path root = new File(getClass().getResource("/scss/").toURI()).toPath();

final BundleOptions options = new BundleOptionsBuilder()
.withWorkDir(root)
.withEsConfig(new EsBuildConfigBuilder().entryNames("[name]").build())
.withDependencies(getJars(List.of("/mvnpm/stimulus-3.2.1.jar", "/mvnpm/bootstrap-5.2.3.jar")),
WebDependencyType.MVNPM)
.addEntryPoint("app.js").build();

Bundler.bundle(options, true);

assertTrue(options.getWorkDir().resolve("dist").resolve("app.js").toFile().exists());
assertTrue(options.getWorkDir().resolve("dist").resolve("app.css").toFile().exists());

}

@Test
public void shouldBundleMvnpmAndCreatePackageJson() throws URISyntaxException, IOException {
executeTest("/mvnpm/stimulus-3.2.0.jar", WebDependencyType.MVNPM, "application-mvnpm.js", true);
executeTest(List.of("/mvnpm/stimulus-3.2.0.jar"), WebDependencyType.MVNPM, "application-mvnpm.js", true);
}

@Test
public void shouldBundleMvnpmWithoutPackageJson() throws URISyntaxException, IOException {
executeTest("/mvnpm/polymer-3.5.1.jar", WebDependencyType.MVNPM, "application-mvnpm-importmap.js", true);
executeTest(List.of("/mvnpm/polymer-3.5.1.jar"), WebDependencyType.MVNPM, "application-mvnpm-importmap.js", true);
}

@Test
public void shouldBundle() throws URISyntaxException, IOException {
executeTest("/webjars/htmx.org-1.8.4.jar", WebDependencyType.WEBJARS, "application-webjar.js", true);
executeTest(List.of("/webjars/htmx.org-1.8.4.jar"), WebDependencyType.WEBJARS, "application-webjar.js", true);
}

@Test
public void shouldWatch() throws URISyntaxException, IOException, InterruptedException {
// given
final BundleOptions options = getBundleOptions("/mvnpm/stimulus-3.2.1.jar", WebDependencyType.MVNPM,
"application-mvnpm.js");
final BundleOptions options = getBundleOptions(List.of("/mvnpm/stimulus-3.2.1.jar"),
WebDependencyType.MVNPM,
"application-mvnpm.js").build();

// when
AtomicBoolean isCalled = new AtomicBoolean(false);
Expand All @@ -64,7 +85,7 @@ public void shouldWatch() throws URISyntaxException, IOException, InterruptedExc
@Test
public void shouldThrowException() {
assertThrows(BundleException.class, () -> {
executeTest("/mvnpm/stimulus-3.2.1.jar", WebDependencyType.MVNPM, "application-error.js", false);
executeTest(List.of("/mvnpm/stimulus-3.2.1.jar"), WebDependencyType.MVNPM, "application-error.js", false);
});
}

Expand All @@ -82,9 +103,9 @@ public void shouldResolveRelativeFolders() throws URISyntaxException, IOExceptio
assertTrue(result.dist().toFile().exists());
}

private void executeTest(String jarName, WebDependencyType type, String scriptName, boolean check)
private void executeTest(List<String> jarNames, WebDependencyType type, String scriptName, boolean check)
throws URISyntaxException, IOException {
final BundleOptions bundleOptions = getBundleOptions(jarName, type, scriptName);
final BundleOptions bundleOptions = getBundleOptions(jarNames, type, scriptName).build();
final BundleResult result = Bundler.bundle(bundleOptions, true);

if (check) {
Expand All @@ -93,13 +114,27 @@ private void executeTest(String jarName, WebDependencyType type, String scriptNa

}

private BundleOptions getBundleOptions(String jarName, WebDependencyType type, String scriptName)
private BundleOptionsBuilder getBundleOptions(List<String> jarNames, WebDependencyType type, String scriptName)
throws URISyntaxException {
final File jar = new File(getClass().getResource(jarName).toURI());
final List<Path> dependencies = Collections.singletonList(jar.toPath());
final List<Path> jars = getJars(jarNames);
final Path rootDir = new File(getClass().getResource("/").toURI()).toPath();
return new BundleOptionsBuilder().withDependencies(dependencies, type)
.addEntryPoint(rootDir, scriptName).build();
return new BundleOptionsBuilder().withDependencies(jars, type)
.addEntryPoint(rootDir, scriptName);
}

private List<Path> getJars(List<String> jarNames) {
final List<Path> jars = jarNames.stream().map(jarName -> {
try {
final URL resource = getClass().getResource(jarName);
if (resource == null) {
throw new RuntimeException("Could not find resource: " + jarName);
}
return new File(resource.toURI()).toPath();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}).toList();
return jars;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.mvnpm.esbuild.resolve;

import static io.mvnpm.esbuild.resolve.BaseResolver.EXECUTABLE_PATH;
import static io.mvnpm.esbuild.resolve.BundleResolverTest.THROWING_RESOLVER;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -52,7 +51,7 @@ public void resolve() throws IOException {

protected static Path createEsBuildBinary(String version) throws IOException {
final Path destination = BaseResolver.createDestination(version);
final Path exec = destination.resolve(EXECUTABLE_PATH);
final Path exec = destination.resolve(BaseResolver.resolveExecutablePath());
Files.createDirectories(exec.getParent());
Files.writeString(exec, "hello");
exec.toFile().setExecutable(true, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.junit.jupiter.api.BeforeAll;
Expand All @@ -23,6 +24,6 @@ public void download() throws IOException {
final Path path = new DownloadResolver(THROWING_RESOLVER).resolve(TEST_VERSION);

// then
assertTrue(path.toFile().exists());
assertTrue(Files.exists(path));
}
}
Binary file added src/test/resources/mvnpm/bootstrap-5.2.3.jar
Binary file not shown.
11 changes: 11 additions & 0 deletions src/test/resources/scss/_style2.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@import "style2";

.foo {
background-color: #ffffff;

.bar {
font-size: 2rem;
color: #333333;
text-align: center;
}
}
5 changes: 5 additions & 0 deletions src/test/resources/scss/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Application } from "@hotwired/stimulus";
import 'bootstrap/scss/bootstrap.scss';
import './style.scss';

Application.start();
9 changes: 9 additions & 0 deletions src/test/resources/scss/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
body {
background-color: white;

.count {
font-size: 2rem;
color: #000000;
text-align: center;
}
}

0 comments on commit e405e68

Please sign in to comment.