Skip to content

Commit

Permalink
Fix error when absolute files sent as positional arguments on a linux…
Browse files Browse the repository at this point in the history
… system (#234)

* Fix

* Fix error when absolute files sent as positional arguments on a linux system

+ Optimize codeNarc baseDir

* Update test case
  • Loading branch information
nvuillam authored Aug 14, 2022
1 parent e1a6602 commit ee9b747
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## UNRELEASED

## [10.0.2] 2022-08-15

- Fix error when absolute files sent as positional arguments on a linux system ([#232](https://github.com/nvuillam/npm-groovy-lint/issues/232))
- Improve performances by calculating the longest command directory to send as base path to CodeNarc

## [10.0.1] 2022-08-14

- Fix error when files sent as positional arguments ([#232](https://github.com/nvuillam/npm-groovy-lint/issues/232))
Expand Down
42 changes: 33 additions & 9 deletions lib/codenarc-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"use strict";

const debug = require("debug")("npm-groovy-lint");
const commondir = require("commondir");
const fs = require("fs-extra");
const os = require("os");
const path = require("path");
Expand Down Expand Up @@ -55,8 +56,11 @@ async function prepareCodeNarcCall(options) {

// Define base directory
const baseBefore = (cnPath !== "." && cnPath.startsWith("/")) || cnPath.includes(":/") || cnPath.includes(":\\") ? "" : process.cwd() + "/";
result.codeNarcBaseDir = cnPath !== "." ? baseBefore + cnPath.replace(/^"(.*)"$/, "$1") : process.cwd();
result.codeNarcBaseDir = path.resolve(result.codeNarcBaseDir);
const codeNarcBaseDir = positionalArgs.length > 0 ?
(await getCodeNarcBaseDirFromFiles(positionalArgs)) :
cnPath !== "." ? baseBefore + cnPath.replace(/^"(.*)"$/, "$1") :
process.cwd();
result.codeNarcBaseDir = path.resolve(codeNarcBaseDir);
result.codenarcArgs.push(`-basedir=${result.codeNarcBaseDir}`);

// Create ruleSet groovy file if necessary
Expand Down Expand Up @@ -90,17 +94,18 @@ async function prepareCodeNarcCall(options) {
if (directoryExists(resolvedPath)) {
finalPattern = "**/" + path.normalize(pathname.replace(/[/\\]$/u, "")).replace(/\\/gu, "/") + filePatterns;
}
// Absolute or cwd - relative path file
if (fs.existsSync(finalPattern)) {
// Relative with codeNarcBaseDir
else if (fs.existsSync(path.join(result.codeNarcBaseDir, finalPattern))) {
const absolutePath = path.resolve(finalPattern).replace(/\\/gu, "/");
const relativePath = path.relative(process.cwd(), path.resolve(finalPattern)).replace(/\\/gu, "/");
fileList.push(absolutePath);
const relativePath = finalPattern.replace(/\\/gu, "/");
finalPattern = "**/" + path.normalize(relativePath.replace(/[/\\]$/u, "")).replace(/\\/gu, "/");
}
// Relative with codeNardBaseDir
else if (fs.existsSync(path.join(result.codeNarcBaseDir, finalPattern))) {
const relativePath = path.relative(process.cwd(), path.join(result.codeNarcBaseDir, finalPattern)).replace(/\\/gu, "/");
fileList.push(relativePath);
// Absolute or cwd - relative path file
else if (fs.existsSync(finalPattern)) {
const absolutePath = path.resolve(finalPattern).replace(/\\/gu, "/");
fileList.push(absolutePath);
const relativePath = path.relative(result.codeNarcBaseDir, path.resolve(finalPattern)).replace(/\\/gu, "/");
finalPattern = "**/" + path.normalize(relativePath.replace(/[/\\]$/u, "")).replace(/\\/gu, "/");
}
// Directory or ant pattern
Expand Down Expand Up @@ -174,6 +179,25 @@ async function prepareCodeNarcCall(options) {
return result;
}

// Calculate longest base dir by analyzing the list of files
async function getCodeNarcBaseDirFromFiles(positionalArgs) {
// All arguments are not files
if (!positionalArgs.every(fileOrDirOrPattern => fs.existsSync(fileOrDirOrPattern) || directoryExists(fileOrDirOrPattern))) {
return process.cwd()
}
const folders = positionalArgs.map((fileOrDir) => {
// Dir
if (directoryExists(fileOrDir)) {
return path.resolve(fileOrDir);
}
// File dir
const fileAbsolute = path.resolve(fileOrDir);
return path.dirname(fileAbsolute);
});
const baseDirFromFiles = commondir(folders);
return baseDirFromFiles
}

// Parse XML result file as js object
async function parseCodeNarcResult(options, codeNarcBaseDir, codeNarcJsonResult, tmpGroovyFileName, parseErrors) {
if (!codeNarcJsonResult || !codeNarcJsonResult.codeNarc || !codeNarcJsonResult.packages) {
Expand Down
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"axios": "^0.21.1",
"chalk": "^4.1.2",
"cli-progress": "^3.10.0",
"commondir": "^1.0.1",
"debug": "^4.1.1",
"decode-html": "^2.0.0",
"find-java-home": "^1.1.0",
Expand Down
6 changes: 2 additions & 4 deletions test/lint-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,9 @@ describe("Lint with API", () => {
const linter = await new NpmGroovyLint([
process.execPath,
"",
"--path",
"./lib/example",
"--verbose",
SAMPLE_FILE_SMALL,
SAMPLE_FILE_WITH_SPACES
path.join("./lib/example",SAMPLE_FILE_SMALL),
path.join("./lib/example",SAMPLE_FILE_WITH_SPACES)
], {
verbose: true
}).run();
Expand Down

0 comments on commit ee9b747

Please sign in to comment.