Skip to content

Fix build on Windows #990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void recipeUpdatesBootDependenciesAndParentVersion() throws IOException {

sut.apply(projectContext);

assertThat(expectedPom).isEqualTo(projectContext.getBuildFile().print());
assertThat(expectedPom).isEqualToNormalizingNewlines(projectContext.getBuildFile().print());

SpringBootApplicationProperties applicationProperties = projectContext.search(new SpringBootApplicationPropertiesResourceListFilter()).get(0);
assertThat(applicationProperties.hasChanges()).isFalse();
Expand Down Expand Up @@ -200,7 +200,7 @@ public void recipeJpaInitPropertyAdded() throws IOException {
);

SpringBootApplicationProperties applicationProperties = projectContext.search(new SpringBootApplicationPropertiesResourceListFilter()).get(0);
assertThat(applicationProperties.getProperty("spring.datasource.initialization-order").get()).isEqualTo("after-jpa");
assertThat(applicationProperties.getProperty("spring.datasource.initialization-order")).hasValue("after-jpa");

verify(ui).askUserYesOrNo(InitDataSourceAfterJpaInitAction.QUESTION);
verifyNoMoreInteractions(ui);
Expand All @@ -218,7 +218,7 @@ public void recipeJpaInitPropertyAddedForSchemaSql() throws IOException {
// sut.apply(projectContext);
//
// SpringBootApplicationProperties applicationProperties = projectContext.getFilteredResources(new SpringBootApplicationPropertiesResourceListFilter()).get(0);
// assertThat(applicationProperties.getProperty("spring.datasource.initialization-order").get()).isEqualTo("after-jpa");
// assertThat(applicationProperties.getProperty("spring.datasource.initialization-order")).hasValue("after-jpa");
//
// verify(ui).askUserYesOrNo(InitDataSourceAfterJpaInitAction.QUESTION);
// verifyNoMoreInteractions(ui);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -50,6 +49,7 @@ public static Path getResultDir(String toDirName) {
}

private static final String TARGET_DIR = "./target/testcode";
private static final String TEMPLATES = "./src/main/resources/templates";
@Setter(AccessLevel.PRIVATE)
private Path rootDir;
@Setter(AccessLevel.PRIVATE)
Expand All @@ -68,10 +68,10 @@ public static RecipeIntegrationTestSupport withTargetDir(Path targetDir) {
public static RecipeIntegrationTestSupport initializeProject(Path sourceDir, String targetDirName) {
try {
Resource classPathResource = new FileSystemResource(sourceDir.toString());
File to = getResultDir(targetDirName).toFile();
File to = getResultDir(targetDirName).toFile().getCanonicalFile();
FileUtils.deleteDirectory(to);
FileUtils.forceMkdir(to);
FileUtils.copyDirectory(classPathResource.getFile(), to);
FileUtils.copyDirectory(classPathResource.getFile().getCanonicalFile(), to);
RecipeIntegrationTestSupport recipeIntegrationTestSupport = new RecipeIntegrationTestSupport();
recipeIntegrationTestSupport.setRootDir(to.toPath());
recipeIntegrationTestSupport.setSourceDir(sourceDir);
Expand All @@ -91,9 +91,11 @@ public static RecipeIntegrationTestSupport initializeProject(String targetDirNam

public void andApplyRecipe(String recipeName) {
SpringBeanProvider.run(ctx -> {
if (new File("./src/main/resources/templates").exists()) {
Configuration configuration = ctx.getBean("configuration", Configuration.class); // FIXME: two freemarker configurations exist
configuration.setDirectoryForTemplateLoading(new File("./src/main/resources/templates"));
Path templatesPath = Path.of(TEMPLATES).normalize().toAbsolutePath();
if (Files.exists(templatesPath)) {
// FIXME: two freemarker configurations exist
Configuration configuration = ctx.getBean("configuration", Configuration.class);
configuration.setDirectoryForTemplateLoading(templatesPath.toFile().getCanonicalFile());
}

ScanCommand scanCommand = ctx.getBean(ScanCommand.class);
Expand All @@ -108,7 +110,7 @@ public void andApplyRecipe(String recipeName) {
Recipe recipe = recipesFound.stream()
.filter(r -> r.getName().equals(recipeName))
.findFirst()
.orElseThrow(() -> new RuntimeException("Could not find recipe '" + recipeName + "' in list of recipes found: " + recipesFound));
.orElseThrow(() -> new RuntimeException("Could not find recipe '%s' in list of recipes found: %s".formatted(recipeName, recipesFound)));
ApplyCommand applyCommand = ctx.getBean(ApplyCommand.class);
applyCommand.execute(projectContext, recipe.getName());
},
Expand All @@ -134,12 +136,12 @@ public void andApplyRecipeComparingWithExpected(String recipeName) {
List<Path> result;
try (Stream<Path> walk = Files.walk(expectedProject)) {
result = walk.filter(Files::isRegularFile)
.map(f -> expectedProject.relativize(f))
.collect(Collectors.toList());
.map(expectedProject::relativize)
.toList();
}

result.stream()
.forEach(r -> assertThat(expectedProject.resolve(r)).hasSameTextualContentAs(migratedProject.resolve(r)));
result.forEach(r -> assertThat(expectedProject.resolve(r).normalize().toAbsolutePath())
.hasSameTextualContentAs(migratedProject.resolve(r).normalize().toAbsolutePath()));
} catch (Exception exception) {
throw new RuntimeException(exception);
}
Expand Down
5 changes: 5 additions & 0 deletions components/sbm-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.sbm.engine.recipe.AbstractAction;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
Expand Down Expand Up @@ -59,7 +60,7 @@ public class AddMinimalPomXml extends AbstractAction {
@Override
public void apply(ProjectContext context) {
String projectDir = context.getProjectRootDirectory().toString();
String projectName = projectDir.replace(" ", "-").substring(projectDir.lastIndexOf("/") + 1);
String projectName = projectDir.replace(" ", "-").substring(projectDir.lastIndexOf(File.separator) + 1);
Map<String, String> params = new HashMap<>();
params.put("groupId", "com.example.change");
params.put("artifactId", projectName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.sbm.java.api.Member;
import org.springframework.sbm.java.impl.OpenRewriteType;

import java.io.File;
import java.nio.file.Path;
import java.util.List;

Expand All @@ -36,7 +37,7 @@ public void apply(ProjectContext context) {
Path sourcePath = module.getBaseJavaSourceLocation().getSourceFolder();
Path sourceDir = rootDirectory.relativize(sourcePath);
module.getMainJavaSources().stream()
.filter(js -> js.getResource().getAbsolutePath().endsWith(sourceDir.resolve(js.getPackageName().replace(".", "/") + "/" + js.getResource().getAbsolutePath().getFileName())))
.filter(js -> filterPath(js, sourceDir))
.map(JavaSource::getTypes)
.flatMap(List::stream)
.filter(t -> OpenRewriteType.class.isAssignableFrom(t.getClass()))
Expand All @@ -52,6 +53,12 @@ public void apply(ProjectContext context) {

}

private static boolean filterPath(JavaSource js, Path sourceDir) {
String packageName = js.getPackageName().replace(".", File.separator);
Path resolved = sourceDir.resolve(packageName).resolve(js.getResource().getAbsolutePath().getFileName());
return js.getResource().getAbsolutePath().endsWith(resolved);
}

private boolean filterActions(OpenRewriteType javaSource) {
Class<?> aClass = getaClass(javaSource);
return Action.class.isAssignableFrom(aClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class ProjectContext {
private final RewriteMigrationResultMerger resultMerger;

public ProjectContext(JavaRefactoringFactory javaRefactoringFactory, Path projectRootDirectory, ProjectResourceSet projectResources, BasePackageCalculator basePackageCalculator, JavaParser javaParser, ExecutionContext executionContext, RewriteMigrationResultMerger resultMerger) {
this.projectRootDirectory = projectRootDirectory.toAbsolutePath();
this.projectRootDirectory = projectRootDirectory.normalize().toAbsolutePath();
this.projectResources = projectResources;
this.javaRefactoringFactory = javaRefactoringFactory;
this.basePackageCalculator = basePackageCalculator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.io.Resource;
import org.springframework.sbm.engine.events.StartedScanningProjectResourceEvent;
import org.springframework.sbm.utils.LinuxWindowsPathUnifier;
import org.springframework.stereotype.Component;

import java.io.IOException;
Expand Down Expand Up @@ -107,7 +108,7 @@ public List<SourceFile> parse(Path baseDir, List<Resource> relevantResources, Li
parserAndParserInputMappings.put(plainTextParser, new ArrayList<>());

parserInputs.forEach(r -> {
Parser parser = parserAndParserInputMappings.keySet().stream()
Parser<?> parser = parserAndParserInputMappings.keySet().stream()
.filter(p -> p.accept(r))
.findFirst()
.orElseThrow(() -> new RuntimeException("Could not find matching parser for " + r.getPath()));
Expand Down Expand Up @@ -141,15 +142,15 @@ private Stream<SourceFile> getSourceFileStream(Path baseDir, ExecutionContext ct
.getValue()
.stream()
.map(resource -> (List<SourceFile>) parseSingleResource(baseDir, ctx, e, resource))
.flatMap(elem -> Stream.ofNullable(elem))
.flatMap(Stream::ofNullable)
.flatMap(List::stream);
}

private List<? extends SourceFile> parseSingleResource(Path baseDir, ExecutionContext ctx, Map.Entry<Parser<? extends SourceFile>, List<Parser.Input>> e, Parser.Input resource) {
try {
return e.getKey().parseInputs(List.of(resource), baseDir, ctx);
} catch(Exception ex) {
if(resource.getPath().toString().contains("src/test/resources")) {
if (LinuxWindowsPathUnifier.unifyPath(resource.getPath()).contains("src/test/resources")) {
log.error("Could not parse resource '%s' using parser %s. Exception was: %s".formatted(resource.getPath(), e.getKey().getClass().getName(), ex.getMessage()));
return null;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

// TODO: make package private
Expand Down Expand Up @@ -65,63 +63,18 @@ public int size() {
return projectResources.size();
}

// /**
// * @deprecated use {@link ProjectContext#getFilteredResources(ProjectResourcesFilter)}
// * with {@link org.springframework.sbm.project.resource.filter.GenericTypeFilter}
// */
// @Deprecated(forRemoval = true)
// public <T> List<RewriteSourceFileHolder<T>> getProjections(Class<T> projectionClass) {
// return typeFilteredList(projectionClass);
// }

public int indexOf(Path absolutePath) {
return projectResources.stream()
.map(ProjectResource::getAbsolutePath)
.collect(Collectors.toList())
.toList()
.indexOf(absolutePath);
}

void clearDeletedResources() {
Iterator<RewriteSourceFileHolder<? extends SourceFile>> iterator = this.projectResources.iterator();
while(iterator.hasNext()) {
RewriteSourceFileHolder<? extends SourceFile> current = iterator.next();
if(current.isDeleted()) {
iterator.remove();
}
}
this.projectResources.removeIf(BaseProjectResource::isDeleted);
}

public Stream<RewriteSourceFileHolder<? extends SourceFile>> streamIncludingDeleted() {
return projectResources.stream();
}
//
// public Stream<ProjectResource> filteredStream(ProjectResourceFilter filter) {
// return projectResources.stream()
// .filter(filter::satisfies);
// }

// public <T extends ProjectResource> Stream<T> classFilteredStream(Class<T> clazz) {
// return filteredStream(pr -> clazz.isAssignableFrom(pr.getClass()))
// .map(clazz::cast);
// }
//
// public <T> Stream<RewriteSourceFileHolder<T>> typeFilteredStream(Class<T> type) {
// return filteredStream(pr -> Objects.nonNull(pr.getType()) && type.isAssignableFrom(pr.getType()))
// .map(pr -> (RewriteSourceFileHolder<T>)pr);
// }
//
// public List<ProjectResource> filteredList(ProjectResourceFilter filter) {
// return filteredStream(filter)
// .collect(Collectors.toList());
// }
//
// public <T extends ProjectResource> List<T> classFilteredList(Class<T> clazz) {
// return classFilteredStream(clazz)
// .collect(Collectors.toList());
// }
//
// public <T> List<RewriteSourceFileHolder<T>> typeFilteredList(Class<T> type) {
// return typeFilteredStream(type)
// .collect(Collectors.toList());
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@
*/
package org.springframework.sbm.build;

import org.assertj.core.api.Assertions;
import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.openrewrite.maven.MavenParser;
import org.openrewrite.maven.internal.RawPom;
import org.openrewrite.maven.tree.Dependency;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.Pom;
import org.openrewrite.xml.tree.Xml;
import org.springframework.sbm.GitHubIssue;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.TestProjectContext;

import java.io.ByteArrayInputStream;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -83,9 +79,9 @@ void scanMultiModuleProjectWithOptionalPropertyProvidedByParent() {

List<Xml.Document> poms = MavenParser.builder().build().parse(pomA, pomB);

assertThat(
poms.get(1).getMarkers().findFirst(MavenResolutionResult.class).get().getPom().getProperties().get("boolean-variable")
).isEqualTo("false");
assertThat(poms.get(1).getMarkers().findFirst(MavenResolutionResult.class))
.hasValueSatisfying(mrr -> assertThat(mrr.getPom().getProperties()).containsEntry("boolean-variable", "false")
);

List<Dependency> requestedDependencies = poms.get(1).getMarkers().findFirst(MavenResolutionResult.class).get().getPom().getRequestedDependencies();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ void withNoClassesInSrcTestJava_providesEmptyProjectResources() {

ProjectContext context = builder.build();

verifySearchTest(context, projectResourceSet -> {
assertThat(projectResourceSet.list()).isEmpty();
}, "");
verifySearchTest(context, projectResourceSet -> assertThat(projectResourceSet.list()).isEmpty(), "");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2134,9 +2134,9 @@ void deserializePluginConfiguration() {
plugin.getArtifactId().equals("maven-compiler-plugin"))
.findAny().orElseThrow();

assertThat(compilerPlugin.getConfiguration().getDeclaredStringValue("source").get()).isEqualTo("${source}");
assertThat(compilerPlugin.getConfiguration().getDeclaredStringValue("target").get()).isEqualTo("17");
assertThat(compilerPlugin.getConfiguration().getDeclaredStringValue("fork").get()).isEqualTo("false");
assertThat(compilerPlugin.getConfiguration().getDeclaredStringValue("source")).hasValue("${source}");
assertThat(compilerPlugin.getConfiguration().getDeclaredStringValue("target")).hasValue("17");
assertThat(compilerPlugin.getConfiguration().getDeclaredStringValue("fork")).hasValue("false");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;
import org.springframework.sbm.project.resource.TestProjectContext;
import org.springframework.sbm.test.TestProjectContextInfo;
import org.springframework.sbm.utils.LinuxWindowsPathUnifier;

import java.util.List;

Expand Down Expand Up @@ -53,9 +54,9 @@ void test_renameMe() {
List<RewriteSourceFileHolder<? extends SourceFile>> parsedResources = context.getProjectResources().list();
assertThat(parsedResources).hasSize(3);
assertThat(parsedResources.get(0).getSourcePath().toString()).isEqualTo("pom.xml");
assertThat(parsedResources.get(1).getSourcePath().toString()).isEqualTo("src/test/resources/one.yaml");
assertThat(LinuxWindowsPathUnifier.unifyPath(parsedResources.get(1).getSourcePath())).isEqualTo("src/test/resources/one.yaml");
// src/test/resources/error.yaml is ignored
assertThat(parsedResources.get(2).getSourcePath().toString()).isEqualTo("src/test/resources/three.yaml");
assertThat(LinuxWindowsPathUnifier.unifyPath(parsedResources.get(2).getSourcePath())).isEqualTo("src/test/resources/three.yaml");
ParsingExecutionContextView contextView = ParsingExecutionContextView.view(projectContextInfo.executionContext());
assertThat(contextView.getParseFailures()).hasSize(1);
assertThat(contextView.getParseFailures().get(0).getText()).isEqualTo("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void parse() {
String xml = "<foo>content</foo>";
RewriteSourceFileHolder<Xml.Document> parse = sut.parse(Path.of(".").toAbsolutePath(), Path.of("some.xml"), xml);
assertThat(parse.getSourceFile().getRoot().getName()).isEqualTo("foo");
assertThat(parse.getSourceFile().getRoot().getValue().get()).isEqualTo("content");
assertThat(parse.getSourceFile().getRoot().getValue()).hasValue("content");
}

@Test
Expand All @@ -51,7 +51,7 @@ void testParse() {
List<RewriteSourceFileHolder<Xml.Document>> rewriteSourceFileHolders = sut.parse(List.of(file), Path.of("./testcode").toAbsolutePath().normalize(), new RewriteExecutionContext());
RewriteSourceFileHolder<Xml.Document> sourceFileHolder = rewriteSourceFileHolders.get(0);
assertThat(sourceFileHolder.getSourceFile().getRoot().getName()).isEqualTo("shiporder");
assertThat(sourceFileHolder.getSourceFile().getRoot().getChild("orderperson").get().getValue().get()).isEqualTo("John Smith");
assertThat(sourceFileHolder.getSourceFile().getRoot().getChild("orderperson").get().getValue()).hasValue("John Smith");
}

@Test
Expand Down
Loading