Skip to content

Commit

Permalink
If WebJar version is a "-snapshot", overwrite existing files when ext…
Browse files Browse the repository at this point in the history
…racing
  • Loading branch information
mkurz committed Jun 8, 2024
1 parent 8cbd500 commit f3e6258
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/main/java/org/webjars/WebJarExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Map;
Expand Down Expand Up @@ -138,10 +139,11 @@ private static void extractResource(@Nonnull String webJarName, @Nonnull WebJarI
final String newPath = resource.getPath().substring(prefix.length());
final String relativeName = String.format("%s%s%s", webJarId, File.separator, newPath);
final File newFile = new File(to, relativeName);
if (!newFile.exists()) {
final boolean allowOverwrite = webJarInfo.getVersion() != null && webJarInfo.getVersion().toLowerCase().endsWith("-snapshot");
if (!newFile.exists() || allowOverwrite) {
try {
newFile.getParentFile().mkdirs();
Files.copy(inputStream, newFile.toPath());
Files.copy(inputStream, newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
inputStream.close();
Set<PosixFilePermission> resourcePerms = resource.getPosixFilePermissions();
if (resourcePerms != null && Files.getFileStore(newFile.toPath()).supportsFileAttributeView(PosixFileAttributeView.class)) {
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/webjars/WebJarAssetLocatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,13 @@ public void invalid_webjar_path_should_return_null() {
public void should_get_a_list_of_webjars() {
Map<String, String> webjars = new WebJarAssetLocator().getWebJars();

assertThat(webjars.size(), is(38)); // this is the pom.xml ones plus the test resources (spaces, foo, bar-node, multiple)
assertThat(webjars.size(), is(39)); // this is the pom.xml ones plus the test resources (spaces, foo, bar-node, multiple)
assertThat(webjars.get("bootstrap"), is("3.1.1"));
assertThat(webjars.get("less-node"), is("1.6.0"));
assertThat(webjars.get("jquery"), is("2.1.0"));
assertThat(webjars.get("angularjs"), is("1.2.11"));
assertThat(webjars.get("virtual-keyboard"), is("1.30.1"));
assertThat(webjars.get("wip"), is("1.0.0-SNAPSHOT"));
}

@Test
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/webjars/WebJarExtractorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public void extractWebJarShouldNotExtractWhenFileExists() throws Exception {
assertFileContains(file, "Hello");
}

@Test
public void extractWebJarShouldExtractWhenFileExistsAndWebJarVersionIsSnapshot() throws Exception {
WebJarExtractor extractor = new WebJarExtractor(createClassLoader());
File cacheDir = createTmpDir();
File file = new File(cacheDir, "wip/caramba.js");
createFile(file, "Hello");
extractor.extractWebJarTo("wip", cacheDir);
assertFileContains(file, "var just = 'do it';");
}

@Test
public void extractAllWebJarsShouldExtractWhenFileDoesntExist() throws Exception {
WebJarExtractor extractor = new WebJarExtractor(createClassLoader());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var just = 'do it';
Empty file.

0 comments on commit f3e6258

Please sign in to comment.