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

add ut for scala command arguments #588

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions src/main/java/scala_maven/ScalaMojoSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public abstract class ScalaMojoSupport extends AbstractMojo {

/** Scala 's version to use. (property 'maven.scala.version' replaced by 'scala.version') */
@Parameter(property = "scala.version")
private String scalaVersion;
protected String scalaVersion;

/**
* Organization/group ID of the Scala used in the project. Default value is 'org.scala-lang'. This
Expand Down Expand Up @@ -497,11 +497,15 @@ private DependencyNodeFilter createScalaDistroDependencyFilter() throws Exceptio
final JavaMainCaller getScalaCommand(final boolean forkOverride, final String mainClass)
throws Exception {
JavaMainCaller cmd = getEmptyScalaCommand(mainClass, forkOverride);
populateArgs(cmd);
return cmd;
}

final void populateArgs(final JavaMainCaller cmd) throws Exception {
for (String option : getScalacOptions()) {
cmd.addArgs(option);
}
cmd.addJvmArgs(jvmArgs);
return cmd;
}

/**
Expand Down
89 changes: 89 additions & 0 deletions src/test/java/scala_maven/ScalaMojoSupportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
package scala_maven;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.List;
import org.junit.Test;
import scala_maven_executions.JavaMainCallerSupport;
import scala_maven_executions.SpawnMonitor;

public class ScalaMojoSupportTest {

Expand Down Expand Up @@ -60,4 +66,87 @@ public void scala2_13_should_generate_non_prefixed_target() {
assertEquals("11", ScalaMojoSupport.targetOption("11", new VersionNumber("2.13.8")));
assertEquals("17", ScalaMojoSupport.targetOption("17", new VersionNumber("2.13.8")));
}

static class ScalaMojoSupportWithRelease extends ScalaMojoSupport {
public ScalaMojoSupportWithRelease() {
this.release = "42";
this.target = "8";
}

public List<String> getScalacOptions() throws Exception {
return super.getScalacOptions();
}

public void setScalaVersion(final String v) {
this.scalaVersion = v;
}

@Override
protected void doExecute() throws Exception {}
}

static class JavaMainCallerArgs extends JavaMainCallerSupport {
public JavaMainCallerArgs() {
super(null, null, null, null, null);
}

public List<String> getArgs() {
return this.args;
}

@Override
public SpawnMonitor spawn(boolean displayCmd) throws Exception {
return null;
}

@Override
public boolean run(boolean displayCmd, boolean throwFailure) throws Exception {
return false;
}

@Override
public void redirectToLog() {}
}

final ScalaMojoSupportWithRelease mojoWithRelease = new ScalaMojoSupportWithRelease();

@Test
public void scala2_11_should_skip_release_option() throws Exception {
mojoWithRelease.setScalaVersion("2.11.0");
List<String> opts = mojoWithRelease.getScalacOptions();
assertNotNull(opts);
assertFalse(opts.contains("-release"));
assertFalse(opts.contains("42"));
}

@Test
public void scala2_12_should_skip_release_option() throws Exception {
mojoWithRelease.setScalaVersion("2.12.0");
List<String> opts = mojoWithRelease.getScalacOptions();
assertNotNull(opts);
assertTrue(opts.contains("-release"));
assertTrue(opts.contains("42"));
}

@Test
public void scala2_13_should_keep_release_option() throws Exception {
mojoWithRelease.setScalaVersion("2.13.0");
List<String> opts = mojoWithRelease.getScalacOptions();
assertNotNull(opts);
assertTrue(opts.contains("-release"));
assertTrue(opts.contains("42"));
}

@Test
public void scala2_12_scala_command_contain_target_and_release() throws Exception {
mojoWithRelease.setScalaVersion("2.12.0");
final JavaMainCallerArgs caller = new JavaMainCallerArgs();
mojoWithRelease.populateArgs(caller);

assertNotNull(caller.getArgs());
assertEquals(3, caller.getArgs().size());
assertTrue(caller.getArgs().contains("-release"));
assertTrue(caller.getArgs().contains("42"));
assertTrue(caller.getArgs().contains("-target:jvm-1.8"));
}
}