From 8e0473771547e00b4142d0afe03391efbcf220dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Wed, 17 Aug 2022 19:14:52 -0300 Subject: [PATCH] Add --use-version tests --- .../cli/commands/internal/PmdCommandTest.java | 59 +++++++++++++------ 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/pmd-cli/src/test/java/net/sourceforge/pmd/cli/commands/internal/PmdCommandTest.java b/pmd-cli/src/test/java/net/sourceforge/pmd/cli/commands/internal/PmdCommandTest.java index bbe0f0d768..a751a12e9c 100644 --- a/pmd-cli/src/test/java/net/sourceforge/pmd/cli/commands/internal/PmdCommandTest.java +++ b/pmd-cli/src/test/java/net/sourceforge/pmd/cli/commands/internal/PmdCommandTest.java @@ -16,22 +16,40 @@ import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; import net.sourceforge.pmd.PMDConfiguration; +import net.sourceforge.pmd.lang.DummyLanguageModule; +import net.sourceforge.pmd.lang.LanguageVersion; import picocli.CommandLine; import picocli.CommandLine.ParseResult; class PmdCommandTest { -// @Test -// void testVersion() throws Exception { -// PMDParameters parameters = new PMDParameters(); -// // no language set, uses default language -// assertEquals("1.7", parameters.getVersion()); -// -// // now set language -// FieldUtils.writeDeclaredField(parameters, "language", "dummy2", true); -// assertEquals("1.0", parameters.getVersion()); -// } + @Test + void testVersionDefault() throws Exception { + final PmdCommand cmd = setupAndParse("--use-version", "dummy", "-d", "a", "-R", "x.xml"); + final LanguageVersion dummyLatest = cmd.toConfiguration().getLanguageVersionOfFile("foo.dummy"); + + // LanguageVersion do not implement equals, but we can check their string representations + assertEquals(DummyLanguageModule.getInstance().getDefaultVersion().toString(), dummyLatest.toString()); + } + + @Test + void testVersionLatest() throws Exception { + final PmdCommand cmd = setupAndParse("--use-version", "dummy-latest", "-d", "a", "-R", "x.xml"); + final LanguageVersion dummyLatest = cmd.toConfiguration().getLanguageVersionOfFile("foo.dummy"); + + // LanguageVersion do not implement equals, but we can check their string representations + assertEquals(DummyLanguageModule.getInstance().getDefaultVersion().toString(), dummyLatest.toString()); + } + + @Test + void testVersionGiven() throws Exception { + final PmdCommand cmd = setupAndParse("--use-version", "dummy-1.2", "-d", "a", "-R", "x.xml"); + final LanguageVersion dummyLatest = cmd.toConfiguration().getLanguageVersionOfFile("foo.dummy"); + + // LanguageVersion do not implement equals, but we can check their string representations + assertEquals(DummyLanguageModule.getInstance().getVersion("1.2").toString(), dummyLatest.toString()); + } @Test void testMultipleDirsAndRuleSets() { @@ -98,18 +116,21 @@ class PmdCommandTest { } private ParseResult parseCommand(final Object cmd, final String... params) { - // Always run against dummy language final List argList = new ArrayList<>(); - argList.add("--use-version"); - argList.add("dummy"); + + // If no language provided, set dummy latest + if (!Arrays.stream(params).anyMatch(s -> s.equals("--use-version"))) { + argList.add("--use-version"); + argList.add("dummy"); + } argList.addAll(Arrays.asList(params)); - final CommandLine commandLine = new CommandLine(cmd) + final CommandLine commandLine = new CommandLine(cmd) .setCaseInsensitiveEnumValuesAllowed(true); - - // Collect errors instead of simply throwing during parsing - commandLine.getCommandSpec().parser().collectErrors(true); - - return commandLine.parseArgs(argList.toArray(new String[0])); + + // Collect errors instead of simply throwing during parsing + commandLine.getCommandSpec().parser().collectErrors(true); + + return commandLine.parseArgs(argList.toArray(new String[0])); } }