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

Test for parsing CLI arguments in lfc #1668

Merged
merged 12 commits into from
Apr 4, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.util.concurrent.Callable;
import java.util.function.Consumer;
Expand All @@ -39,12 +40,16 @@
import org.opentest4j.AssertionFailedError;

import org.lflang.cli.Io;
import org.lflang.cli.Lfc;

import picocli.CommandLine;

/**
* Test utilities for a CLI tool, eg {@link org.lflang.cli.Lfc},
* {@link org.lflang.cli.Lff}.
*
* @author Clément Fournier
* @author Atharva Patil
*/
abstract class CliToolTestFixture {

Expand Down Expand Up @@ -158,4 +163,5 @@ interface ThrowingConsumer<T> {
void accept(T t) throws Exception;
}
}

}
63 changes: 62 additions & 1 deletion org.lflang.tests/src/org/lflang/tests/cli/LfcCliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,30 @@

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.lflang.tests.TestUtils.TempDirBuilder.dirBuilder;
import static org.lflang.tests.TestUtils.TempDirChecker.dirChecker;
import static org.lflang.tests.TestUtils.isDirectory;
import static org.lflang.tests.TestUtils.isRegularFile;

import com.google.inject.Injector;

import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.util.Properties;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import org.lflang.LocalStrings;
import org.lflang.cli.Io;
import org.lflang.cli.Lfc;
import org.lflang.generator.LFGeneratorContext.BuildParm;

/**
* @author Clément Fournier
* @author Atharva Patil
*/
public class LfcCliTest {

Expand Down Expand Up @@ -98,17 +105,71 @@ public void testGenInSrcDir(@TempDir Path tempDir) throws IOException {
.check("bin", isDirectory())
.check("src-gen/File/File.py", isRegularFile());
});
}

@Test
public void testBuildParams(@TempDir Path tempDir)
patilatharva marked this conversation as resolved.
Show resolved Hide resolved
throws IOException {
dirBuilder(tempDir).file("src/File.lf", LF_PYTHON_FILE);

String[] args = {
"src/File.lf",
"--output-path", "src",
"--build-type", "Release",
"--clean",
"--target-compiler", "gcc",
"--external-runtime-path", "src",
"--federated",
"--logging", "4",
"--lint",
"--no-compile",
"--quiet",
"--rti", "-1",
patilatharva marked this conversation as resolved.
Show resolved Hide resolved
"--runtime-version", "rs",
"--scheduler", "2",
"--threading", "false",
"--workers", "1",
};
LfcOneShotTestFixture fixture = new LfcOneShotTestFixture();

fixture.run(tempDir, args)
.verify(result -> {
// Don't validate execution because args are dummy args.
Properties properties = fixture.lfc.cliArgsToBuildParams();
assertEquals(properties.getProperty(BuildParm.BUILD_TYPE.getKey()), "Release");
assertEquals(properties.getProperty(BuildParm.CLEAN.getKey()), "true");
assertEquals(properties.getProperty(BuildParm.EXTERNAL_RUNTIME_PATH.getKey()), "src");
assertEquals(properties.getProperty(BuildParm.LINT.getKey()), "true");
assertEquals(properties.getProperty(BuildParm.LOGGING.getKey()), "4");
assertEquals(properties.getProperty(BuildParm.TARGET_COMPILER.getKey()), "gcc");
assertEquals(properties.getProperty(BuildParm.QUIET.getKey()), "true");
assertEquals(properties.getProperty(BuildParm.RTI.getKey()), "-1");
patilatharva marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(properties.getProperty(BuildParm.RUNTIME_VERSION.getKey()), "rs");
assertEquals(properties.getProperty(BuildParm.THREADING.getKey()), "false");
assertEquals(properties.getProperty(BuildParm.WORKERS.getKey()), "1");
});
}


static class LfcTestFixture extends CliToolTestFixture {


@Override
protected void runCliProgram(Io io, String[] args) {
Lfc.main(io, args);
}
}

static class LfcOneShotTestFixture extends CliToolTestFixture {

private Lfc lfc;

@Override
protected void runCliProgram(Io io, String[] args) {
// Injector used to obtain Main instance.
final Injector injector = Lfc.getInjector("lfc", io);
// Main instance.
this.lfc = injector.getInstance(Lfc.class);
lfc.doExecute(io, args);
}
}
}
9 changes: 7 additions & 2 deletions org.lflang/src/org/lflang/cli/CliBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ protected static void cliMain(
// Main instance.
final CliBase main = injector.getInstance(toolClass);
// Parse arguments and execute main logic.
CommandLine cmd = new CommandLine(main)
main.doExecute(io, args);
}

public void doExecute(Io io, String[] args) {
CommandLine cmd = new CommandLine(this)
.setOut(new PrintWriter(io.getOut()))
.setErr(new PrintWriter(io.getErr()));
int exitCode = cmd.execute(args);
Expand All @@ -111,7 +115,7 @@ protected static void cliMain(
*/
public abstract void run();

protected static Injector getInjector(String toolName, Io io) {
public static Injector getInjector(String toolName, Io io) {
final ReportingBackend reporter
= new ReportingBackend(io, toolName + ": ");

Expand Down Expand Up @@ -247,4 +251,5 @@ public Resource getResource(Path path) {
return null;
}
}

}
4 changes: 2 additions & 2 deletions org.lflang/src/org/lflang/cli/Lfc.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void run() {
List<Path> paths = getInputPaths();
final Path outputRoot = getOutputRoot();
// Hard code the props based on the options we want.
Properties properties = this.filterPassOnProps();
Properties properties = this.cliArgsToBuildParams();

try {
// Invoke the generator on all input file paths.
Expand Down Expand Up @@ -229,7 +229,7 @@ private Path getActualOutputPath(Path root, Path path) {
*
* @return Properties for the code generator.
*/
protected Properties filterPassOnProps() {
public Properties cliArgsToBuildParams() {
patilatharva marked this conversation as resolved.
Show resolved Hide resolved
Properties props = new Properties();

if (buildType != null) {
Expand Down