Skip to content

Commit

Permalink
Fix ignored paths checking
Browse files Browse the repository at this point in the history
Seems since using files API in pascalgn#54, `IGNORED` stopped to work
  • Loading branch information
mat3e committed Oct 4, 2024
1 parent 49850f3 commit fe0f2f9
Show file tree
Hide file tree
Showing 5 changed files with 585 additions and 24 deletions.
24 changes: 13 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function debug(...str) {
}

function parseIgnored(str = "") {
const ignored = str
const ignored = (str || "")
.split(/\r|\n/)
.map(s => s.trim())
.filter(s => s.length > 0 && !s.startsWith("#"))
Expand All @@ -154,14 +154,13 @@ function parseIgnored(str = "") {
if (path == null || path === "/dev/null") {
return true;
}
const pathname = path.slice(2);
let ignore = false;
for (const entry of ignored) {
if (entry.not) {
if (pathname.match(entry.not.regex)) {
if (path.match(entry.not.regex)) {
return false;
}
} else if (!ignore && pathname.match(entry.regex)) {
} else if (!ignore && path.match(entry.regex)) {
ignore = true;
}
}
Expand All @@ -184,12 +183,15 @@ async function readFile(path) {

function getChangedLines(isIgnored, pullRequestFiles) {
return pullRequestFiles
.map(file =>
isIgnored(file.filename) &&
(!file.previous_filename || isIgnored(file.previous_filename))
? 0
: file.changes
)
.map(file => {
const ignored = isIgnored(file.previous_filename) && isIgnored(file.filename);
if (ignored) {
console.log(`${file.filename} is ignored`);
} else {
console.log(`${file.filename} is not ignored`);
}
return ignored ? 0 : file.changes;
})
.reduce((total, current) => total + current, 0);
}

Expand Down Expand Up @@ -238,7 +240,7 @@ if (require.main === require.cache[eval('__filename')]) {
);
}

module.exports = { main };
module.exports = { main, parseIgnored };


/***/ }),
Expand Down
24 changes: 13 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function debug(...str) {
}

function parseIgnored(str = "") {
const ignored = str
const ignored = (str || "")
.split(/\r|\n/)
.map(s => s.trim())
.filter(s => s.length > 0 && !s.startsWith("#"))
Expand All @@ -148,14 +148,13 @@ function parseIgnored(str = "") {
if (path == null || path === "/dev/null") {
return true;
}
const pathname = path.slice(2);
let ignore = false;
for (const entry of ignored) {
if (entry.not) {
if (pathname.match(entry.not.regex)) {
if (path.match(entry.not.regex)) {
return false;
}
} else if (!ignore && pathname.match(entry.regex)) {
} else if (!ignore && path.match(entry.regex)) {
ignore = true;
}
}
Expand All @@ -178,12 +177,15 @@ async function readFile(path) {

function getChangedLines(isIgnored, pullRequestFiles) {
return pullRequestFiles
.map(file =>
isIgnored(file.filename) &&
(!file.previous_filename || isIgnored(file.previous_filename))
? 0
: file.changes
)
.map(file => {
const ignored = isIgnored(file.previous_filename) && isIgnored(file.filename);
if (ignored) {
console.log(`${file.filename} is ignored`);
} else {
console.log(`${file.filename} is not ignored`);
}
return ignored ? 0 : file.changes;
})
.reduce((total, current) => total + current, 0);
}

Expand Down Expand Up @@ -232,4 +234,4 @@ if (require.main === module) {
);
}

module.exports = { main };
module.exports = { main, parseIgnored };
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"size-label-action": "index.js"
},
"scripts": {
"test": "yarn compile && node test.js",
"test": "vitest run && yarn compile && node test.js",
"lint": "eslint .",
"compile": "ncc build index.js --license LICENSE -o dist",
"prepublish": "yarn compile"
Expand All @@ -24,7 +24,8 @@
"@vercel/ncc": "^0.36.1",
"dotenv": "^16.1.2",
"eslint": "^8.41.0",
"tmp": "^0.2.1"
"tmp": "^0.2.1",
"vitest": "^2.1.2"
},
"prettier": {
"arrowParens": "avoid",
Expand Down
36 changes: 36 additions & 0 deletions parseIgnored.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, it } from "vitest";
import { parseIgnored } from "./index";

describe("parseIgnored", () => {
it.each(["", null, undefined, "\r\n", "\n", "#", "#file"])("doesn't ignore when no patterns to ignore provided (%s)", (input) => {
// when
const isIgnored = parseIgnored(input);

// then
expect(isIgnored("file")).toBe(false);
// always ignored
expect(isIgnored(null)).toBe(true);
expect(isIgnored("/dev/null")).toBe(true);
});

it("ignores ordinary patterns", () => {
// when
const isIgnored = parseIgnored("**/src/integration/**\n**/src/test/**\n**/src/testFixtures/**");

// then
expect(isIgnored("file")).toBe(false);
expect(isIgnored("src/test/file")).toBe(true);
expect(isIgnored("codebase/src/testFixtures/file")).toBe(true);
});

it("accepts negated patterns", () => {
// when
const isIgnored = parseIgnored(".*\n!.gitignore\nyarn.lock\ngenerated/**");

// then
expect(isIgnored(".git")).toBe(true);
expect(isIgnored(".gitignore")).toBe(false);
expect(isIgnored("yarn.lock")).toBe(true);
expect(isIgnored("generated/source")).toBe(true);
});
});
Loading

0 comments on commit fe0f2f9

Please sign in to comment.