From 64399d08413865466b4319a01ebd6f159923c22e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 29 Mar 2026 07:01:23 +0000 Subject: [PATCH] test: add 3 unit tests for findFiles name-priority, depth-priority, and empty-names Documents the exact selection behaviour of findFiles when multiple config-file candidates are present, which is relied on by getStandard() to honour PHPCS config-file priority (.phpcs.xml beats phpcs.xml). New tests: - 'returns first matching name when multiple config files exist in same directory': verifies that .phpcs.xml is preferred over phpcs.xml when both are present, matching PHPCS priority rules. - 'prefers file in deeper directory over ancestor when names overlap': explicit counterpart to the existing depth test using the same name in root and sub-dir, making the deepest-first behaviour unambiguous. - 'returns null for empty names array': defensive edge case; ensures an empty candidates list never throws and always returns null. All 10 tests pass (node --test test/unit.test.js). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- test/unit.test.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/unit.test.js b/test/unit.test.js index 0fb759e..7203aa9 100644 --- a/test/unit.test.js +++ b/test/unit.test.js @@ -103,4 +103,32 @@ describe("findFiles", () => { const result = findFiles(path.join(tmpRoot, "single"), ".", "phpcs.xml"); assert.equal(result, expected); }); + + test("returns first matching name when multiple config files exist in same directory", () => { + // When both .phpcs.xml and phpcs.xml exist, .phpcs.xml (first in the + // names array) should be returned — matching PHPCS priority rules. + mkFile("priority", "sub", ".phpcs.xml"); + mkFile("priority", "sub", "phpcs.xml"); + const result = findFiles( + path.join(tmpRoot, "priority"), + "sub", + [".phpcs.xml", "phpcs.xml", "phpcs.xml.dist"] + ); + assert.equal(result, path.join(tmpRoot, "priority", "sub", ".phpcs.xml")); + }); + + test("prefers file in deeper directory over ancestor when names overlap", () => { + // Both the workspace root and a sub-directory contain phpcs.xml. + // The deepest match should be returned because we search from deepest upward. + mkFile("deeperwin", "phpcs.xml"); // at root + const deeper = mkFile("deeperwin", "a", "phpcs.xml"); // in sub-dir + const result = findFiles(path.join(tmpRoot, "deeperwin"), "a", "phpcs.xml"); + assert.equal(result, deeper); + }); + + test("returns null for empty names array", () => { + mkDir("emptynames", "sub"); + const result = findFiles(path.join(tmpRoot, "emptynames"), "sub", []); + assert.equal(result, null); + }); });