Skip to content

Commit

Permalink
Update JS module
Browse files Browse the repository at this point in the history
  • Loading branch information
oowekyala committed Apr 15, 2022
1 parent c778266 commit 367c6cf
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 24 deletions.
17 changes: 14 additions & 3 deletions pmd-javascript/src/test/java/net/sourceforge/pmd/cli/CLITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,27 @@

import org.junit.Test;

import net.sourceforge.pmd.PMD.StatusCode;

/**
* @author Romain Pelisse <belaran@gmail.com>
*
*/
public class CLITest extends BaseCLITest {
@Test
public void useEcmaScript() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "xml", "-R", "ecmascript-basic", "-version", "3", "-l",
"ecmascript", "-debug", };
String log = runTest(args);
String log = runTest(StatusCode.VIOLATIONS_FOUND,
"-d",
SOURCE_FOLDER,
"-f",
"xml",
"-R",
"rulesets/testing/js-rset1.xml",
"-version",
"3",
"-l",
"ecmascript",
"--debug");
assertThat(log, containsPattern("Adding file .*\\.js \\(lang: ecmascript 3\\)"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/

package net.sourceforge.pmd.lang.ecmascript;

import java.util.List;

import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule;

/**
* @author Clément Fournier
*/
public class DummyJsRule extends AbstractEcmascriptRule {

@Override
public void apply(List<? extends Node> nodes, RuleContext ctx) {
for (Node node : nodes) {
apply(node, ctx);
}
}

public void apply(Node node, RuleContext ctx) {

}

public static class DummyRuleOneViolationPerFile extends DummyJsRule {

@Override
public void apply(Node node, RuleContext ctx) {
ctx.addViolation(node);
}
}

}
25 changes: 25 additions & 0 deletions pmd-javascript/src/test/resources/rulesets/testing/js-rset1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<ruleset name="Test Ruleset" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">

<description>
Ruleset used by test RuleSetReferenceIdTest
</description>

<rule name="DummyRuleWithAViolationPerFile"
language="ecmascript"
since="1.0"
message="Violation from test-rset-1.xml"
class="net.sourceforge.pmd.lang.ecmascript.DummyJsRule$DummyRuleOneViolationPerFile">
<description>
Just for test
</description>
<priority>3</priority>
<example>
<![CDATA[
]]>
</example>
</rule>

</ruleset>
33 changes: 12 additions & 21 deletions pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCLITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
import java.nio.file.Files;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;
import org.apache.tools.ant.util.TeeOutputStream;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.contrib.java.lang.system.SystemOutRule;

import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.PMD.StatusCode;
Expand All @@ -39,8 +40,10 @@ public abstract class BaseCLITest {
// and slowing down tests
protected static final String SOURCE_FOLDER = "src/test/resources/net/sourceforge/pmd/cli";

protected PrintStream originalOut;
protected PrintStream originalErr;
@Rule
public SystemErrRule systemErrRule = new SystemErrRule().muteForSuccessfulTests();
@Rule
public SystemOutRule systemOutRule = new SystemOutRule().muteForSuccessfulTests();

/**
* @throws java.lang.Exception
Expand All @@ -55,20 +58,6 @@ public static void setUp() throws Exception {
}
}

@Before
public void setup() {
originalOut = System.out;
originalErr = System.err;
}

@After
public void tearDown() {
IOUtils.closeQuietly(System.out);

System.setOut(originalOut);
System.setErr(originalErr);
}

protected void createTestOutputFile(String filename) {
try {
@SuppressWarnings("PMD.CloseResource")
Expand Down Expand Up @@ -133,9 +122,11 @@ protected String runTest(int expectedExitCode, String... args) {

protected String runTest(StatusCode expectedExitCode, String... args) {
ByteArrayOutputStream console = new ByteArrayOutputStream();
PrintStream out = new PrintStream(console);

PrintStream out = new PrintStream(new TeeOutputStream(console, System.out));
PrintStream err = new PrintStream(new TeeOutputStream(console, System.err));
System.setOut(out);
System.setErr(out);
System.setErr(err);
StatusCode statusCode = PMD.runPmd(args);
assertEquals(expectedExitCode, statusCode);
return console.toString();
Expand Down

0 comments on commit 367c6cf

Please sign in to comment.