Skip to content

Commit

Permalink
Make test work without another repo
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Aug 14, 2020
1 parent e32d2a1 commit 28fd7e4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
9 changes: 9 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
README.md @orta

# These are all basically the test fixture info
root-codeowners/**/*.js @two

# Collaborators for Portuguese Translation of the Website
packages/playground-examples/copy/pt/**/*.md @khaosdoctor @danilofuchs @orta
packages/tsconfig-reference/copy/pt/**/*.md @khaosdoctor @danilofuchs @orta
packages/typescriptlang-org/src/copy/pt/**/*.md @khaosdoctor @danilofuchs @orta
packages/documentation/copy/pt/**/*.ts @khaosdoctor @danilofuchs @orta
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ function getFilesNotOwnedByCodeOwner(owner, files, cwd) {
const codeowners = new Codeowners(cwd);

for (const file of files) {
let owners = codeowners.getOwner(file);
const relative = file.startsWith("/") ? file.slice(1) : file
let owners = codeowners.getOwner(relative);
if (!owners.includes(owner)) {
filesWhichArentOwned.push(file)
}
Expand All @@ -128,12 +129,13 @@ function getFilesNotOwnedByCodeOwner(owner, files, cwd) {

function listFilesWithOwners(files, cwd) {
const codeowners = new Codeowners(cwd);
console.log("Known code-owners for changed files:")
console.log("\nKnown code-owners for changed files:")
for (const file of files) {
let owners = codeowners.getOwner(file);
const relative = file.startsWith("/") ? file.slice(1) : file
let owners = codeowners.getOwner(relative);
console.log(`- ${file} (${new Intl.ListFormat().format(owners)})`)
}
console.log("> CODEOWNERS file:")
console.log("\n> CODEOWNERS file:")
console.log(readFileSync(codeowners.codeownersFilePath, "utf8"))
}

Expand All @@ -142,7 +144,8 @@ function findCodeOwnersForChangedFiles(changedFiles, cwd) {
const codeowners = new Codeowners(cwd);

for (const file of changedFiles) {
const filesOwners = codeowners.getOwner(file);
const relative = file.startsWith("/") ? file.slice(1) : file
const filesOwners = codeowners.getOwner(relative);
filesOwners.forEach(o => owners.add(o))
}

Expand Down
29 changes: 17 additions & 12 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
const {getFilesNotOwnedByCodeOwner, findCodeOwnersForChangedFiles} = require(".")
const { getFilesNotOwnedByCodeOwner, findCodeOwnersForChangedFiles } = require(".");

test("determine who owns a set of files", () => {
const noFiles = findCodeOwnersForChangedFiles(["root-codeowners/one.two.js"], "./test-code-owners-repo")
expect(noFiles).toEqual(["@two"])
const noFiles = findCodeOwnersForChangedFiles(["root-codeowners/one.two.js"], "./test-code-owners-repo");
expect(noFiles).toEqual(["@two"]);

const filesNotInCodeowners = findCodeOwnersForChangedFiles(["root-codeowners/one.two.ts"], "./test-code-owners-repo")
expect(filesNotInCodeowners).toEqual([])
})
const filesNotInCodeowners = findCodeOwnersForChangedFiles(["root-codeowners/one.two.ts"], "./test-code-owners-repo");
expect(filesNotInCodeowners).toEqual([]);
});

test("deciding if someone has access to merge", () => {
const noFiles = getFilesNotOwnedByCodeOwner("@two", ["root-codeowners/one.two.js"], "./test-code-owners-repo")
expect(noFiles).toEqual([])
test("real world", () => {
const changed = ["/packages/tsconfig-reference/copy/pt/options/files.md"];
const filesNotInCodeowners = findCodeOwnersForChangedFiles(changed, ".");
expect(filesNotInCodeowners).toEqual(["@khaosdoctor", "@danilofuchs", "@orta"]);
});

const filesNotInCodeowners = getFilesNotOwnedByCodeOwner("@two", ["random-path/file.ts"], "./test-code-owners-repo")
expect(filesNotInCodeowners).toEqual(["random-path/file.ts"])
})
test("deciding if someone has access to merge", () => {
const noFiles = getFilesNotOwnedByCodeOwner("@two", ["root-codeowners/one.two.js"], "./test-code-owners-repo");
expect(noFiles).toEqual([]);

const filesNotInCodeowners = getFilesNotOwnedByCodeOwner("@two", ["random-path/file.ts"], "./test-code-owners-repo");
expect(filesNotInCodeowners).toEqual(["random-path/file.ts"]);
});

0 comments on commit 28fd7e4

Please sign in to comment.