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 b79a6da
Show file tree
Hide file tree
Showing 5 changed files with 647 additions and 29 deletions.
90 changes: 74 additions & 16 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
/******/ var __webpack_modules__ = ({

/***/ 2932:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
/***/ ((module, __webpack_exports__, __nccwpck_require__) => {

"use strict";
__nccwpck_require__.r(__webpack_exports__);
/* harmony export */ __nccwpck_require__.d(__webpack_exports__, {
/* harmony export */ "parseIgnored": () => (/* binding */ parseIgnored)
/* harmony export */ });
/* module decorator */ module = __nccwpck_require__.hmd(module);

const fs = __nccwpck_require__(7147);
const process = __nccwpck_require__(7282);
Expand Down Expand Up @@ -140,8 +146,9 @@ function debug(...str) {
}
}

// visible for testing
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 +161,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 +190,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 @@ -228,7 +237,7 @@ function getSizesInput() {
}
}

if (require.main === require.cache[eval('__filename')]) {
if (__nccwpck_require__.c[__nccwpck_require__.s] === module) {
main().then(
() => (process.exitCode = 0),
e => {
Expand Down Expand Up @@ -9341,8 +9350,8 @@ module.exports = JSON.parse('[[[0,44],"disallowed_STD3_valid"],[[45,46],"valid"]
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ id: moduleId,
/******/ loaded: false,
/******/ exports: {}
/******/ };
/******/
Expand All @@ -9355,21 +9364,70 @@ module.exports = JSON.parse('[[[0,44],"disallowed_STD3_valid"],[[45,46],"valid"]
/******/ if(threw) delete __webpack_module_cache__[moduleId];
/******/ }
/******/
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/ // expose the module cache
/******/ __nccwpck_require__.c = __webpack_module_cache__;
/******/
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __nccwpck_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__nccwpck_require__.o(definition, key) && !__nccwpck_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/harmony module decorator */
/******/ (() => {
/******/ __nccwpck_require__.hmd = (module) => {
/******/ module = Object.create(module);
/******/ if (!module.children) module.children = [];
/******/ Object.defineProperty(module, 'exports', {
/******/ enumerable: true,
/******/ set: () => {
/******/ throw new Error('ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: ' + module.id);
/******/ }
/******/ });
/******/ return module;
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __nccwpck_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __nccwpck_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
/******/
/************************************************************************/
/******/
/******/ // module cache are used so entry inlining is disabled
/******/ // startup
/******/ // Load entry module and return exports
/******/ // This entry module is referenced by other modules so it can't be inlined
/******/ var __webpack_exports__ = __nccwpck_require__(2932);
/******/ var __webpack_exports__ = __nccwpck_require__(__nccwpck_require__.s = 2932);
/******/ module.exports = __webpack_exports__;
/******/
/******/ })()
Expand Down
25 changes: 14 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ function debug(...str) {
}
}

function parseIgnored(str = "") {
const ignored = str
// visible for testing
export function parseIgnored(str = "") {
const ignored = (str || "")
.split(/\r|\n/)
.map(s => s.trim())
.filter(s => s.length > 0 && !s.startsWith("#"))
Expand All @@ -148,14 +149,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 +178,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
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 b79a6da

Please sign in to comment.