mirror of
https://gitee.com/dcren/initializr.git
synced 2025-07-18 22:35:48 +08:00
Refactor support for testing executable files
Previously, hasExecutableFile only checked if the file exists and not if it was executable. This commit ensures that the file exists and is executable using ExecTask as Unzip and Untar do not preserve file permissions. See gh-937
This commit is contained in:
parent
f16971fcd3
commit
292e47e98b
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package io.spring.initializr.generator.spring.test;
|
package io.spring.initializr.generator.spring.test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
@ -192,11 +193,17 @@ public class ProjectAssert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ProjectAssert hasExecutableFile(String... localPaths) {
|
public ProjectAssert hasExecutableFile(String... localPaths) {
|
||||||
|
if (runningOnWindows()) {
|
||||||
for (String localPath : localPaths) {
|
for (String localPath : localPaths) {
|
||||||
assertFile(localPath, true);
|
assertFile(localPath, true);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
for (String localPath : localPaths) {
|
||||||
|
assertExecutable(localPath, true);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public ProjectAssert hasNoFile(String... localPaths) {
|
public ProjectAssert hasNoFile(String... localPaths) {
|
||||||
assertThat(this.projectStructure.getRelativePathsOfProjectFiles()).doesNotContain(localPaths);
|
assertThat(this.projectStructure.getRelativePathsOfProjectFiles()).doesNotContain(localPaths);
|
||||||
@ -210,6 +217,19 @@ public class ProjectAssert {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ProjectAssert assertExecutable(String localPath, boolean exist) {
|
||||||
|
Path candidate = this.projectStructure.resolve(localPath);
|
||||||
|
assertThat(Files.exists(candidate)).describedAs("Invalid presence (%s) exist for %s", exist, localPath)
|
||||||
|
.isEqualTo(exist);
|
||||||
|
assertThat(Files.isExecutable(candidate)).describedAs("File (%s) is not an executable", localPath)
|
||||||
|
.isEqualTo(exist);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean runningOnWindows() {
|
||||||
|
return File.separatorChar == '\\';
|
||||||
|
}
|
||||||
|
|
||||||
private Properties properties(String localPath) {
|
private Properties properties(String localPath) {
|
||||||
Path file = this.projectStructure.resolve(localPath);
|
Path file = this.projectStructure.resolve(localPath);
|
||||||
try {
|
try {
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package io.spring.initializr.web;
|
package io.spring.initializr.web;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
@ -32,6 +33,7 @@ import io.spring.initializr.web.AbstractInitializrIntegrationTests.Config;
|
|||||||
import io.spring.initializr.web.mapper.InitializrMetadataVersion;
|
import io.spring.initializr.web.mapper.InitializrMetadataVersion;
|
||||||
import io.spring.initializr.web.support.InitializrMetadataUpdateStrategy;
|
import io.spring.initializr.web.support.InitializrMetadataUpdateStrategy;
|
||||||
import org.apache.tools.ant.Project;
|
import org.apache.tools.ant.Project;
|
||||||
|
import org.apache.tools.ant.taskdefs.ExecTask;
|
||||||
import org.apache.tools.ant.taskdefs.Expand;
|
import org.apache.tools.ant.taskdefs.Expand;
|
||||||
import org.apache.tools.ant.taskdefs.Untar;
|
import org.apache.tools.ant.taskdefs.Untar;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
@ -211,6 +213,18 @@ public abstract class AbstractInitializrIntegrationTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void untar(Path archiveFile, Path project) {
|
private void untar(Path archiveFile, Path project) {
|
||||||
|
if (!runningOnWindows()) {
|
||||||
|
createProjectDir(project);
|
||||||
|
ExecTask execTask = new ExecTask();
|
||||||
|
execTask.setProject(new Project());
|
||||||
|
execTask.setExecutable("tar");
|
||||||
|
execTask.createArg().setValue("-C");
|
||||||
|
execTask.createArg().setValue(project.toFile().getAbsolutePath());
|
||||||
|
execTask.createArg().setValue("-xf");
|
||||||
|
execTask.createArg().setValue(archiveFile.toFile().getAbsolutePath());
|
||||||
|
execTask.execute();
|
||||||
|
}
|
||||||
|
else {
|
||||||
Untar expand = new Untar();
|
Untar expand = new Untar();
|
||||||
expand.setProject(new Project());
|
expand.setProject(new Project());
|
||||||
expand.setDest(project.toFile());
|
expand.setDest(project.toFile());
|
||||||
@ -220,14 +234,38 @@ public abstract class AbstractInitializrIntegrationTests {
|
|||||||
expand.setCompression(method);
|
expand.setCompression(method);
|
||||||
expand.execute();
|
expand.execute();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createProjectDir(Path project) {
|
||||||
|
ExecTask execTask = new ExecTask();
|
||||||
|
execTask.setProject(new Project());
|
||||||
|
execTask.setExecutable("mkdir");
|
||||||
|
execTask.createArg().setValue(project.toFile().getAbsolutePath());
|
||||||
|
execTask.execute();
|
||||||
|
}
|
||||||
|
|
||||||
private void unzip(Path archiveFile, Path project) {
|
private void unzip(Path archiveFile, Path project) {
|
||||||
|
if (!runningOnWindows()) {
|
||||||
|
ExecTask execTask = new ExecTask();
|
||||||
|
execTask.setProject(new Project());
|
||||||
|
execTask.setExecutable("unzip");
|
||||||
|
execTask.createArg().setValue(archiveFile.toFile().getAbsolutePath());
|
||||||
|
execTask.createArg().setValue("-d");
|
||||||
|
execTask.createArg().setValue(project.toFile().getAbsolutePath());
|
||||||
|
execTask.execute();
|
||||||
|
}
|
||||||
|
else {
|
||||||
Expand expand = new Expand();
|
Expand expand = new Expand();
|
||||||
expand.setProject(new Project());
|
expand.setProject(new Project());
|
||||||
expand.setDest(project.toFile());
|
expand.setDest(project.toFile());
|
||||||
expand.setSrc(archiveFile.toFile());
|
expand.setSrc(archiveFile.toFile());
|
||||||
expand.execute();
|
expand.execute();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean runningOnWindows() {
|
||||||
|
return File.separatorChar == '\\';
|
||||||
|
}
|
||||||
|
|
||||||
protected Path writeArchive(byte[] body) throws IOException {
|
protected Path writeArchive(byte[] body) throws IOException {
|
||||||
Path archiveFile = this.folder.resolve("archive");
|
Path archiveFile = this.folder.resolve("archive");
|
||||||
|
Loading…
Reference in New Issue
Block a user