Skip to content
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

Fix NPE when empty working dir #1208

Merged
merged 4 commits into from
Mar 7, 2017
Merged
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 @@ -392,7 +392,7 @@ protected CompilationUnitDeclaration[] buildUnits(JDTBuilder jdtBuilder, SpoonFo
.classpathOptions(new ClasspathOptions().encoding(this.encoding).classpath(classpath)) //
.complianceOptions(new ComplianceOptions().compliance(javaCompliance)) //
.advancedOptions(new AdvancedOptions().preserveUnusedVars().continueExecution().enableJavadoc()) //
.sources(new SourceOptions().sources()) // no sources, handled by the JDTBatchCompiler
.sources(new SourceOptions().sources(sourcesFolder.getAllJavaFiles())) // no sources, handled by the JDTBatchCompiler
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the unrelated comment

.build();
} else {
args = jdtBuilder.build();
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/spoon/LauncherTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package spoon;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import spoon.compiler.Environment;
import spoon.reflect.visitor.DefaultJavaPrettyPrinter;
import spoon.support.JavaOutputProcessor;

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

Expand Down Expand Up @@ -68,4 +71,25 @@ public void testInitEnvironment() throws Exception {

}

@Test
public void testLauncherInEmptyWorkingDir() throws Exception {

// Contract: Spoon can be launched in an empty folder as a working directory
// See: https://github.com/INRIA/spoon/pull/1208
// This test does not fail (it's not enough to change user.dir we should launch process inside that dir) but it explains the problem
final Launcher launcher = new Launcher();
Path path = Files.createTempDirectory("emptydir");

String oldUserDir = System.getProperty("user.dir");
System.setProperty("user.dir", path.toFile().getAbsolutePath());

// path should exist, otherwise it would crash on a filenotfoundexception before showing the bug
launcher.addInputResource(oldUserDir+"/src/test/java/spoon/LauncherTest.java");
try {
launcher.buildModel();
} finally {
System.setProperty("user.dir", oldUserDir);
}
}

}