Skip to content

Commit

Permalink
Set env PMD_APEX_ROOT_DIRECTORY
Browse files Browse the repository at this point in the history
Fixes #157
  • Loading branch information
adangel committed Oct 24, 2024
1 parent 7dfff86 commit 6fd2b94
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/lib/apexPmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ export class ApexPmd {
}
}

// TODO: determine the nearest sfdx-project.json file beginning from targetPath up to workspaceRootPath
env["PMD_APEX_ROOT_DIRECTORY"] = workspaceRootPath;

const cmd = `"${path.join(pmdBinPath, 'bin', 'pmd')}" check ${pmdKeys}`;

this.outputChannel.appendLine(`node: ${process.version}`);
Expand Down
8 changes: 8 additions & 0 deletions test/assets/project3_unusedmethod/custom_ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name="Test ruleset" xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>Test ruleset</description>

<rule ref="category/apex/design.xml/UnusedMethod" />

</ruleset>
9 changes: 9 additions & 0 deletions test/assets/project3_unusedmethod/sfdx-project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"packageDirectories": [
{
"path": "src",
"default": true
}
],
"namespace": "foo_ns"
}
9 changes: 9 additions & 0 deletions test/assets/project3_unusedmethod/src/Foo.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Foo {
// this method is needed, otherwise the whole class is considered as unused, as it would contain
// then only one unused method.
private void other() {}

public void unusedMethod() {
other();
}
}
5 changes: 5 additions & 0 deletions test/assets/project3_unusedmethod/src/Foo.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>50.0</apiVersion>
<status>Active</status>
</ApexClass>
34 changes: 34 additions & 0 deletions test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,38 @@ suite('Extension Tests', () => {
done(e);
});
});

test('UnusedMethod with Apex Link and PMD_APEX_ROOT_DIRECTORY', function (done) {
this.timeout(100000);

const workspaceRootPath = path.join(TEST_ASSETS_PATH, 'project3_unusedmethod');
const rulesetPath = path.join(workspaceRootPath, 'custom_ruleset.xml');
const apexClassFile = path.join(workspaceRootPath, 'src', 'Foo.cls');

const collection = vscode.languages.createDiagnosticCollection('apex-pmd-test');

const config = new Config();
config.pmdBinPath = PMD_PATH;
config.rulesets = [rulesetPath];
config.priorityErrorThreshold = 3;
config.priorityWarnThreshold = 1;
config.workspaceRootPath = workspaceRootPath;
config.additionalClassPaths = [];
config.commandBufferSize = 64000000;

const pmd = new ApexPmd(outputChannel, config);

const testApexUri = vscode.Uri.file(apexClassFile);
pmd
.run(apexClassFile, collection)
.then(() => {
const errs = collection.get(testApexUri);
assert.strictEqual(errs.length, 1);
assert.strictEqual(errs[0].message, "Unused methods make understanding code harder (rule: Design-UnusedMethod)");
done();
})
.catch((e) => {
done(e);
});
});
});

0 comments on commit 6fd2b94

Please sign in to comment.