diff --git a/tests/acceptance/8/CRITERIA.json b/tests/acceptance/8/CRITERIA.json new file mode 100644 index 0000000..f9e533a --- /dev/null +++ b/tests/acceptance/8/CRITERIA.json @@ -0,0 +1,18 @@ +{ + "schema": "elffuss-t2t/criteria@2", + "issue": 8, + "criteria": [ + { + "index": 1, + "tests": [ + "tests/acceptance/8/criteria.test.js::criterion 1" + ] + }, + { + "index": 2, + "tests": [ + "tests/acceptance/8/criteria.test.js::criterion 2" + ] + } + ] +} diff --git a/tests/acceptance/8/criteria.test.js b/tests/acceptance/8/criteria.test.js new file mode 100644 index 0000000..3dae187 --- /dev/null +++ b/tests/acceptance/8/criteria.test.js @@ -0,0 +1,27 @@ +import { test } from 'node:test' +import assert from 'node:assert/strict' +import { readFileSync } from 'node:fs' +import * as mod from '../../../web/js/tools/clone.js' // the code under test: call it as mod.(...) +const source = readFileSync(new URL('../../../web/js/tools/clone.js', import.meta.url), 'utf8') // the text of web/js/tools/clone.js, for a criterion about how the file is written + +test('criterion 1', () => { + // `parseRepoUrl("https://github.com/o/r.git/")` returns `{ host: "github", owner: "o", repo: "r", branch: null }`, and `parseRepoUrl("https://github.com/o/r/tree/dev")` still returns branch `"dev"`. + const result1 = mod.parseRepoUrl("https://github.com/o/r.git/") + assert.deepStrictEqual(result1, { host: "github", owner: "o", repo: "r", branch: null }) + + const result2 = mod.parseRepoUrl("https://github.com/o/r/tree/dev") + assert.equal(result2.branch, "dev") +}) + +test('criterion 2', () => { + // `parseRepoUrl("https://bitbucket.org/o/r.git/")` returns `{ host: "bitbucket", owner: "o", repo: "r", branch: null }`, and `parseRepoUrl("https://example.org/o/r")` still throws an Error. + const result1 = mod.parseRepoUrl("https://bitbucket.org/o/r.git/") + assert.deepStrictEqual(result1, { host: "bitbucket", owner: "o", repo: "r", branch: null }) + + try { + mod.parseRepoUrl("https://example.org/o/r") + assert.fail("Expected parseRepoUrl to throw an Error for invalid URL") + } catch (e) { + // Expected error + } +}) diff --git a/web/js/tools/clone.js b/web/js/tools/clone.js index 09ee334..4166d87 100644 --- a/web/js/tools/clone.js +++ b/web/js/tools/clone.js @@ -12,7 +12,7 @@ const MAX_FILE_BYTES = 4 * 1024 * 1024; // ficheros más grandes se saltan (bina const CONCURRENCY = 10; export function parseRepoUrl(raw) { - const url = String(raw || '').trim().replace(/\.git$/, '').replace(/\/+$/, ''); + const url = String(raw || '').trim().replace(/\.git\/?$/, '').replace(/\/+$/, ''); let m = url.match(/^(?:https?:\/\/)?(?:www\.)?github\.com\/([\w.-]+)\/([\w.-]+)(?:\/tree\/([\w./-]+))?/i); if (m) return { host: 'github', owner: m[1], repo: m[2], branch: m[3] || null }; m = url.match(/^(?:https?:\/\/)?(?:www\.)?bitbucket\.org\/([\w.-]+)\/([\w.-]+)(?:\/src\/([\w./-]+))?/i);