Skip to content

Commit cf99f3c

Browse files
committed
Test autodetect.js
1 parent dfba934 commit cf99f3c

File tree

5 files changed

+151
-10
lines changed

5 files changed

+151
-10
lines changed

.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module.exports = {
33
browser: true,
44
jquery: true,
55
"jest/globals": true,
6+
node: true,
7+
es6: true,
68
},
79
extends: ["eslint:recommended"],
810
parser: "@babel/eslint-parser",

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
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: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { getRepoProviders, detect } from "../lib/autodetect";
2+
import { readFileSync } from "node:fs";
3+
4+
const mybinderConfig = JSON.parse(
5+
readFileSync(`${__dirname}/fixtures/mybinder.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 the repo provider configs", async () => {
26+
const config = await getRepoProviders("https://binder.example.org");
27+
expect(config).toEqual(mybinderConfig);
28+
});
29+
30+
test("detect returns null if no provider matches", async () => {
31+
const result = await detect(
32+
"https://binder.example.org",
33+
"https://github.com/binder-examples/conda/pulls",
34+
);
35+
expect(result).toBeNull();
36+
});
37+
38+
test("detect parses a repo with no path", async () => {
39+
const expected = {
40+
providerPrefix: "gh",
41+
repository: "binder-examples/conda",
42+
ref: null,
43+
path: null,
44+
pathType: null,
45+
providerName: "Fake GitHub",
46+
};
47+
const result = await detect(
48+
"https://binder.example.org",
49+
"https://github.com/binder-examples/conda",
50+
);
51+
expect(result).toEqual(expected);
52+
});
53+
54+
test("detect parses a repo with a ref but no path", async () => {
55+
const expected = {
56+
providerPrefix: "gh",
57+
repository: "binder-examples/conda",
58+
ref: "abc",
59+
path: null,
60+
pathType: null,
61+
providerName: "Fake GitHub",
62+
};
63+
const result = await detect(
64+
"https://binder.example.org",
65+
"https://github.com/binder-examples/conda/tree/abc",
66+
);
67+
expect(result).toEqual(expected);
68+
});
69+
70+
test("detect parses a repo with a ref and file path", async () => {
71+
const expected = {
72+
providerPrefix: "gh",
73+
repository: "binder-examples/conda",
74+
ref: "f00a783",
75+
path: "index.ipynb",
76+
pathType: "filepath",
77+
providerName: "Fake GitHub",
78+
};
79+
const result = await detect(
80+
"https://binder.example.org",
81+
"https://github.com/binder-examples/conda/blob/f00a783/index.ipynb",
82+
);
83+
expect(result).toEqual(expected);
84+
});
85+
86+
test("detect parses a repo with a ref and directory path", async () => {
87+
const expected = {
88+
providerPrefix: "gh",
89+
repository: "binder-examples/conda",
90+
ref: "f00a783",
91+
path: ".github/workflows",
92+
pathType: "urlpath",
93+
providerName: "Fake GitHub",
94+
};
95+
const result = await detect(
96+
"https://binder.example.org",
97+
"https://github.com/binder-examples/conda/tree/f00a783/.github/workflows",
98+
);
99+
expect(result).toEqual(expected);
100+
});
101+
102+
test("detect checks other repo providers", async () => {
103+
const expected = {
104+
providerPrefix: "gl",
105+
repository: "gitlab-org/gitlab-foss",
106+
ref: "v16.4.4",
107+
path: "README.md",
108+
pathType: "filepath",
109+
providerName: "GitLab.com",
110+
};
111+
const result = await detect(
112+
"https://binder.example.org",
113+
"https://gitlab.com/gitlab-org/gitlab-foss/-/blob/v16.4.4/README.md",
114+
);
115+
expect(result).toEqual(expected);
116+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"gh": {
3+
"text": "Fake Provider",
4+
"tag_text": "Fake Ref",
5+
"ref_prop_disabled": true,
6+
"label_prop_disabled": true,
7+
"display_name": "Fake GitHub",
8+
"regex_detect": [
9+
"^https://github\\.com/(?<repo>[^/]+/[^/]+)(/blob/(?<ref>[^/]+)(/(?<filepath>.+))?)?$",
10+
"^https://github\\.com/(?<repo>[^/]+/[^/]+)(/tree/(?<ref>[^/]+)(/(?<urlpath>.+))?)?$"
11+
]
12+
},
13+
"gl": {
14+
"text": "GitLab.com repository or URL",
15+
"tag_text": "Git ref (branch, tag, or commit)",
16+
"ref_prop_disabled": false,
17+
"label_prop_disabled": false,
18+
"display_name": "GitLab.com",
19+
"regex_detect": [
20+
"^https://gitlab\\.com/(?<repo>[^/]+/[^/]+(/[^/-][^/]+)*)(/-/blob/(?<ref>[^/]+)(/(?<filepath>.+))?)?$",
21+
"^https://gitlab\\.com/(?<repo>[^/]+/[^/]+(/[^/-][^/]+)*)(/-/tree/(?<ref>[^/]+)(/(?<urlpath>.+))?)?$"
22+
]
23+
},
24+
"nore": {
25+
"text": "Fake No Regex Provider",
26+
"tag_text": "Fake Ref",
27+
"ref_prop_disabled": true,
28+
"label_prop_disabled": true,
29+
"display_name": "Fake No Regex"
30+
}
31+
}

0 commit comments

Comments
 (0)