Skip to content

Commit 1f2b0d5

Browse files
committed
Test autodetect.js
1 parent 7bc6275 commit 1f2b0d5

File tree

4 files changed

+195
-10
lines changed

4 files changed

+195
-10
lines changed

js/packages/binderhub-client/lib/autodetect.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
import { fetch as fetchPolyfill } from "whatwg-fetch";
2-
3-
// Use native browser fetch if available, and use the polyfill if not available
4-
// (e.g. in tests https://github.com/jestjs/jest/issues/13834#issuecomment-1407375787)
5-
// @todo: this is only a problem in the jest tests, so get rid of this and mock fetch instead
6-
const fetch = window.fetch || fetchPolyfill;
7-
81
/**
92
* Dict holding cached values of API request to _config endpoint for base URL
103
*/
@@ -50,7 +43,7 @@ export async function detect(baseUrl, text) {
5043
return {
5144
providerPrefix: provider,
5245
repository: m.groups.repo,
53-
ref: m.groups.ref,
46+
ref: m.groups.ref || null,
5447
path: m.groups.filepath || m.groups.urlpath || null,
5548
pathType: m.groups.filepath
5649
? "filepath"

js/packages/binderhub-client/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
},
1515
"homepage": "https://github.com/jupyterhub/binderhub#readme",
1616
"dependencies": {
17-
"event-iterator": "^2.0.0",
1817
"event-source-polyfill": "^1.0.31",
19-
"whatwg-fetch": "^3.6.19"
18+
"event-iterator": "^2.0.0"
2019
}
2120
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { getRepoProviders, detect } from "../lib/autodetect";
2+
import { readFileSync } from "node:fs";
3+
4+
const mybinderConfig = JSON.parse(
5+
readFileSync(`${__dirname}/fixtures/repoprovider-config.json`, {
6+
encoding: "utf-8",
7+
}),
8+
);
9+
10+
// Mock fetch()
11+
// https://www.leighhalliday.com/mock-fetch-jest
12+
global.fetch = jest.fn((url) => {
13+
if (url == "https://binder.example.org/_config") {
14+
return Promise.resolve({
15+
json: () => Promise.resolve(mybinderConfig),
16+
});
17+
}
18+
return Promise.reject(`Unexpected URL ${url}`);
19+
});
20+
21+
beforeEach(() => {
22+
fetch.mockClear();
23+
});
24+
25+
test("getRepoProviders requests and caches the repo provider configs", async () => {
26+
const config = await getRepoProviders("https://binder.example.org");
27+
expect(config).toEqual(mybinderConfig);
28+
29+
await getRepoProviders("https://binder.example.org");
30+
expect(fetch).toHaveBeenCalledTimes(1);
31+
});
32+
33+
test("detect returns null if no provider matches", async () => {
34+
const result = await detect(
35+
"https://binder.example.org",
36+
"https://github.com/binder-examples/conda/pulls",
37+
);
38+
expect(result).toBeNull();
39+
});
40+
41+
test("detect parses a repo with no path", async () => {
42+
const expected = {
43+
providerPrefix: "gh",
44+
repository: "binder-examples/conda",
45+
ref: null,
46+
path: null,
47+
pathType: null,
48+
providerName: "GitHub",
49+
};
50+
const result = await detect(
51+
"https://binder.example.org",
52+
"https://github.com/binder-examples/conda",
53+
);
54+
expect(result).toEqual(expected);
55+
});
56+
57+
test("detect parses a repo with a ref but no path", async () => {
58+
const expected = {
59+
providerPrefix: "gh",
60+
repository: "binder-examples/conda",
61+
ref: "abc",
62+
path: null,
63+
pathType: null,
64+
providerName: "GitHub",
65+
};
66+
const result = await detect(
67+
"https://binder.example.org",
68+
"https://github.com/binder-examples/conda/tree/abc",
69+
);
70+
expect(result).toEqual(expected);
71+
});
72+
73+
test("detect parses a repo with a ref and file path", async () => {
74+
const expected = {
75+
providerPrefix: "gh",
76+
repository: "binder-examples/conda",
77+
ref: "f00a783",
78+
path: "index.ipynb",
79+
pathType: "filepath",
80+
providerName: "GitHub",
81+
};
82+
const result = await detect(
83+
"https://binder.example.org",
84+
"https://github.com/binder-examples/conda/blob/f00a783/index.ipynb",
85+
);
86+
expect(result).toEqual(expected);
87+
});
88+
89+
test("detect parses a repo with a ref and directory path", async () => {
90+
const expected = {
91+
providerPrefix: "gh",
92+
repository: "binder-examples/conda",
93+
ref: "f00a783",
94+
path: ".github/workflows",
95+
pathType: "urlpath",
96+
providerName: "GitHub",
97+
};
98+
const result = await detect(
99+
"https://binder.example.org",
100+
"https://github.com/binder-examples/conda/tree/f00a783/.github/workflows",
101+
);
102+
expect(result).toEqual(expected);
103+
});
104+
105+
test("detect checks other repo providers", async () => {
106+
const expected = {
107+
providerPrefix: "gl",
108+
repository: "gitlab-org/gitlab-foss",
109+
ref: "v16.4.4",
110+
path: "README.md",
111+
pathType: "filepath",
112+
providerName: "GitLab.com",
113+
};
114+
const result = await detect(
115+
"https://binder.example.org",
116+
"https://gitlab.com/gitlab-org/gitlab-foss/-/blob/v16.4.4/README.md",
117+
);
118+
expect(result).toEqual(expected);
119+
});
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"gh": {
3+
"text": "GitHub repository name or URL",
4+
"tag_text": "Git ref (branch, tag, or commit)",
5+
"ref_prop_disabled": false,
6+
"label_prop_disabled": false,
7+
"display_name": "GitHub",
8+
"regex_detect": [
9+
"^https://github\\.com/(?<repo>[^/]+/[^/]+)(/blob/(?<ref>[^/]+)(/(?<filepath>.+))?)?$",
10+
"^https://github\\.com/(?<repo>[^/]+/[^/]+)(/tree/(?<ref>[^/]+)(/(?<urlpath>.+))?)?$"
11+
]
12+
},
13+
"gist": {
14+
"text": "Gist ID (username/gistId) or URL",
15+
"tag_text": "Git commit SHA",
16+
"ref_prop_disabled": false,
17+
"label_prop_disabled": false,
18+
"display_name": "Gist",
19+
"regex_detect": [
20+
"^https://gist\\.github\\.com/(?<repo>[^/]+/[^/]+)(/(?<ref>[^/]+))?$"
21+
]
22+
},
23+
"git": {
24+
"text": "Arbitrary git repository URL (http://git.example.com/repo)",
25+
"tag_text": "Git ref (branch, tag, or commit)",
26+
"ref_prop_disabled": false,
27+
"label_prop_disabled": false,
28+
"display_name": "Git repository",
29+
"regex_detect": null
30+
},
31+
"gl": {
32+
"text": "GitLab.com repository or URL",
33+
"tag_text": "Git ref (branch, tag, or commit)",
34+
"ref_prop_disabled": false,
35+
"label_prop_disabled": false,
36+
"display_name": "GitLab.com",
37+
"regex_detect": [
38+
"^https://gitlab\\.com/(?<repo>[^/]+/[^/]+(/[^/-][^/]+)*)(/-/blob/(?<ref>[^/]+)(/(?<filepath>.+))?)?$",
39+
"^https://gitlab\\.com/(?<repo>[^/]+/[^/]+(/[^/-][^/]+)*)(/-/tree/(?<ref>[^/]+)(/(?<urlpath>.+))?)?$"
40+
]
41+
},
42+
"zenodo": {
43+
"text": "Zenodo DOI (10.5281/zenodo.3242074)",
44+
"tag_text": "Git ref (branch, tag, or commit)",
45+
"ref_prop_disabled": true,
46+
"label_prop_disabled": true,
47+
"display_name": "Zenodo DOI",
48+
"regex_detect": null
49+
},
50+
"figshare": {
51+
"text": "Figshare DOI (10.6084/m9.figshare.9782777.v1)",
52+
"tag_text": "Git ref (branch, tag, or commit)",
53+
"ref_prop_disabled": true,
54+
"label_prop_disabled": true,
55+
"display_name": "Figshare DOI",
56+
"regex_detect": null
57+
},
58+
"hydroshare": {
59+
"text": "Hydroshare resource id or URL",
60+
"tag_text": "Git ref (branch, tag, or commit)",
61+
"ref_prop_disabled": true,
62+
"label_prop_disabled": true,
63+
"display_name": "Hydroshare resource",
64+
"regex_detect": null
65+
},
66+
"dataverse": {
67+
"text": "Dataverse DOI (10.7910/DVN/TJCLKP)",
68+
"tag_text": "Git ref (branch, tag, or commit)",
69+
"ref_prop_disabled": true,
70+
"label_prop_disabled": true,
71+
"display_name": "Dataverse DOI",
72+
"regex_detect": null
73+
}
74+
}

0 commit comments

Comments
 (0)