-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
150 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { getRepoProviders, detect } from "../lib/autodetect"; | ||
import { readFileSync } from "node:fs"; | ||
|
||
const mybinderConfig = JSON.parse( | ||
readFileSync(`${__dirname}/fixtures/mybinder.config.json`, { | ||
encoding: "utf-8", | ||
}), | ||
); | ||
|
||
// Mock fetch() | ||
// https://www.leighhalliday.com/mock-fetch-jest | ||
global.fetch = jest.fn((url) => { | ||
if (url == "https://binder.example.org/_config") { | ||
return Promise.resolve({ | ||
json: () => Promise.resolve(mybinderConfig), | ||
}); | ||
} | ||
return Promise.reject(`Unexpected URL ${url}`); | ||
}); | ||
|
||
beforeEach(() => { | ||
fetch.mockClear(); | ||
}); | ||
|
||
test("getRepoProviders requests the repo provider configs", async () => { | ||
const config = await getRepoProviders("https://binder.example.org"); | ||
expect(config).toEqual(mybinderConfig); | ||
}); | ||
|
||
test("detect returns null if no provider matches", async () => { | ||
const result = await detect( | ||
"https://binder.example.org", | ||
"https://github.com/binder-examples/conda/pulls", | ||
); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
test("detect parses a repo with no path", async () => { | ||
const expected = { | ||
providerPrefix: "gh", | ||
repository: "binder-examples/conda", | ||
ref: null, | ||
path: null, | ||
pathType: null, | ||
providerName: "Fake GitHub", | ||
}; | ||
const result = await detect( | ||
"https://binder.example.org", | ||
"https://github.com/binder-examples/conda", | ||
); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
test("detect parses a repo with a ref but no path", async () => { | ||
const expected = { | ||
providerPrefix: "gh", | ||
repository: "binder-examples/conda", | ||
ref: "abc", | ||
path: null, | ||
pathType: null, | ||
providerName: "Fake GitHub", | ||
}; | ||
const result = await detect( | ||
"https://binder.example.org", | ||
"https://github.com/binder-examples/conda/tree/abc", | ||
); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
test("detect parses a repo with a ref and file path", async () => { | ||
const expected = { | ||
providerPrefix: "gh", | ||
repository: "binder-examples/conda", | ||
ref: "f00a783", | ||
path: "index.ipynb", | ||
pathType: "filepath", | ||
providerName: "Fake GitHub", | ||
}; | ||
const result = await detect( | ||
"https://binder.example.org", | ||
"https://github.com/binder-examples/conda/blob/f00a783/index.ipynb", | ||
); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
test("detect parses a repo with a ref and directory path", async () => { | ||
const expected = { | ||
providerPrefix: "gh", | ||
repository: "binder-examples/conda", | ||
ref: "f00a783", | ||
path: ".github/workflows", | ||
pathType: "urlpath", | ||
providerName: "Fake GitHub", | ||
}; | ||
const result = await detect( | ||
"https://binder.example.org", | ||
"https://github.com/binder-examples/conda/tree/f00a783/.github/workflows", | ||
); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
test("detect checks other repo providers", async () => { | ||
const expected = { | ||
providerPrefix: "gl", | ||
repository: "gitlab-org/gitlab-foss", | ||
ref: "v16.4.4", | ||
path: "README.md", | ||
pathType: "filepath", | ||
providerName: "GitLab.com", | ||
}; | ||
const result = await detect( | ||
"https://binder.example.org", | ||
"https://gitlab.com/gitlab-org/gitlab-foss/-/blob/v16.4.4/README.md", | ||
); | ||
expect(result).toEqual(expected); | ||
}); |
31 changes: 31 additions & 0 deletions
31
js/packages/binderhub-client/tests/fixtures/mybinder.config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"gh": { | ||
"text": "Fake Provider", | ||
"tag_text": "Fake Ref", | ||
"ref_prop_disabled": true, | ||
"label_prop_disabled": true, | ||
"display_name": "Fake GitHub", | ||
"regex_detect": [ | ||
"^https://github\\.com/(?<repo>[^/]+/[^/]+)(/blob/(?<ref>[^/]+)(/(?<filepath>.+))?)?$", | ||
"^https://github\\.com/(?<repo>[^/]+/[^/]+)(/tree/(?<ref>[^/]+)(/(?<urlpath>.+))?)?$" | ||
] | ||
}, | ||
"gl": { | ||
"text": "GitLab.com repository or URL", | ||
"tag_text": "Git ref (branch, tag, or commit)", | ||
"ref_prop_disabled": false, | ||
"label_prop_disabled": false, | ||
"display_name": "GitLab.com", | ||
"regex_detect": [ | ||
"^https://gitlab\\.com/(?<repo>[^/]+/[^/]+(/[^/-][^/]+)*)(/-/blob/(?<ref>[^/]+)(/(?<filepath>.+))?)?$", | ||
"^https://gitlab\\.com/(?<repo>[^/]+/[^/]+(/[^/-][^/]+)*)(/-/tree/(?<ref>[^/]+)(/(?<urlpath>.+))?)?$" | ||
] | ||
}, | ||
"nore": { | ||
"text": "Fake No Regex Provider", | ||
"tag_text": "Fake Ref", | ||
"ref_prop_disabled": true, | ||
"label_prop_disabled": true, | ||
"display_name": "Fake No Regex" | ||
} | ||
} |