refactor tests to give clearer feedback

Instead of comments in the table test, use the features of jest that allow each
test to give a specific error message. This change also reduces the total number
of tests, but coverage should remain the same branch-wise.

Signed-off-by: Jamie Klassen <jklassen@vmware.com>
This commit is contained in:
Jamie Klassen
2022-07-20 15:54:02 -04:00
parent 7495b78b7e
commit 1de2669c91
+134 -163
View File
@@ -34,13 +34,6 @@ describe('gitlab core', () => {
);
});
const configWithToken: GitLabIntegrationConfig = {
host: 'gitlab.com',
token: '0123456789',
apiBaseUrl: '<ignored>',
baseUrl: '<ignored>',
};
const configWithNoToken: GitLabIntegrationConfig = {
host: 'gitlab.com',
apiBaseUrl: '<ignored>',
@@ -61,163 +54,141 @@ describe('gitlab core', () => {
baseUrl: 'https://gitlab.mycompany.com',
};
describe('getGitLabFileFetchUrl with .yaml extension', () => {
it.each([
// Project URLs
{
config: configWithNoToken,
url: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file.yaml',
result:
'https://gitlab.com/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile.yaml/raw?ref=branch',
},
{
config: configSelfHosteWithRelativePath,
url: 'https://gitlab.mycompany.com/gitlab/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file.yaml',
result:
'https://gitlab.mycompany.com/gitlab/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile.yaml/raw?ref=branch',
},
{
config: configSelfHostedWithoutRelativePath,
url: 'https://gitlab.mycompany.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file.yaml',
result:
'https://gitlab.mycompany.com/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile.yaml/raw?ref=branch',
},
{
config: configWithNoToken,
// Works with non URI encoded link
url: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file with spaces.yaml',
result:
'https://gitlab.com/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile%20with%20spaces.yaml/raw?ref=branch',
},
{
config: configSelfHosteWithRelativePath,
url: 'https://gitlab.mycompany.com/gitlab/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file with spaces.yaml',
result:
'https://gitlab.mycompany.com/gitlab/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile%20with%20spaces.yaml/raw?ref=branch',
},
{
config: configSelfHostedWithoutRelativePath,
url: 'https://gitlab.mycompany.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file with spaces.yaml',
result:
'https://gitlab.mycompany.com/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile%20with%20spaces.yaml/raw?ref=branch',
},
{
config: configWithNoToken,
url: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path%20with%20spaces/to/file.yaml',
result:
'https://gitlab.com/api/v4/projects/12345/repository/files/my%2Fpath%20with%20spaces%2Fto%2Ffile.yaml/raw?ref=branch',
},
{
config: configWithToken,
url: 'https://gitlab.example.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path%20with%20spaces/to/file.yaml',
result:
'https://gitlab.example.com/api/v4/projects/12345/repository/files/my%2Fpath%20with%20spaces%2Fto%2Ffile.yaml/raw?ref=branch',
},
{
config: configWithNoToken,
url: 'https://gitlab.com/groupA/teams/teamA/repoA/-/blob/branch/my/path%20with%20spaces/to/file.yaml', // Repo not in subgroup
result:
'https://gitlab.com/api/v4/projects/12345/repository/files/my%2Fpath%20with%20spaces%2Fto%2Ffile.yaml/raw?ref=branch',
},
// Raw URLs
{
config: configWithNoToken,
url: 'https://gitlab.example.com/a/b/blob/master/c.yaml',
result: 'https://gitlab.example.com/a/b/raw/master/c.yaml',
},
{
config: configWithNoToken,
url: 'https://gitlab.example.com/a/b/repo/blob/master/c.yaml',
result: 'https://gitlab.example.com/a/b/repo/raw/master/c.yaml',
},
{
config: configWithNoToken,
url: 'https://gitlab.example.com/a/blob/blob/master/c.yaml',
result: 'https://gitlab.example.com/a/blob/raw/master/c.yaml',
},
{
config: configWithNoToken,
url: 'https://gitlab.example.com/a/b/blob/blob/c.yaml',
result: 'https://gitlab.example.com/a/b/raw/blob/c.yaml',
},
{
config: configWithNoToken,
url: 'https://gitlab.example.com/a//blob/blob/c.yaml',
result: 'https://gitlab.example.com/a/blob/raw/c.yaml',
},
])('should handle happy path %#', async ({ config, url, result }) => {
await expect(getGitLabFileFetchUrl(url, config)).resolves.toBe(result);
});
});
describe('getGitLabFileFetchUrl', () => {
describe('when target has a scoped route', () => {
it('returns a projects API URL', async () => {
const target =
'https://gitlab.com/group/project/-/blob/branch/folder/file.yaml';
const fetchUrl =
'https://gitlab.com/api/v4/projects/12345/repository/files/folder%2Ffile.yaml/raw?ref=branch';
await expect(
getGitLabFileFetchUrl(target, configWithNoToken),
).resolves.toBe(fetchUrl);
});
describe('getGitLabFileFetchUrl with .yml extension', () => {
it.each([
// Project URLs
{
config: configWithNoToken,
url: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file.yml',
result:
'https://gitlab.com/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile.yml/raw?ref=branch',
},
{
config: configSelfHosteWithRelativePath,
url: 'https://gitlab.mycompany.com/gitlab/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file.yml',
result:
'https://gitlab.mycompany.com/gitlab/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile.yml/raw?ref=branch',
},
{
config: configSelfHostedWithoutRelativePath,
url: 'https://gitlab.mycompany.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file.yml',
result:
'https://gitlab.mycompany.com/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile.yml/raw?ref=branch',
},
{
config: configWithNoToken,
// Works with non URI encoded link
url: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file with spaces.yml',
result:
'https://gitlab.com/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile%20with%20spaces.yml/raw?ref=branch',
},
{
config: configSelfHosteWithRelativePath,
// Works with non URI encoded link
url: 'https://gitlab.mycompany.com/gitlab/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file with spaces.yml',
result:
'https://gitlab.mycompany.com/gitlab/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile%20with%20spaces.yml/raw?ref=branch',
},
{
config: configSelfHostedWithoutRelativePath,
// Works with non URI encoded link
url: 'https://gitlab.mycompany.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path/to/file with spaces.yml',
result:
'https://gitlab.mycompany.com/api/v4/projects/12345/repository/files/my%2Fpath%2Fto%2Ffile%20with%20spaces.yml/raw?ref=branch',
},
{
config: configWithNoToken,
url: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path%20with%20spaces/to/file.yml',
result:
'https://gitlab.com/api/v4/projects/12345/repository/files/my%2Fpath%20with%20spaces%2Fto%2Ffile.yml/raw?ref=branch',
},
{
config: configWithToken,
url: 'https://gitlab.example.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/my/path%20with%20spaces/to/file.yml',
result:
'https://gitlab.example.com/api/v4/projects/12345/repository/files/my%2Fpath%20with%20spaces%2Fto%2Ffile.yml/raw?ref=branch',
},
{
config: configWithNoToken,
url: 'https://gitlab.com/groupA/teams/teamA/repoA/-/blob/branch/my/path%20with%20spaces/to/file.yml', // Repo not in subgroup
result:
'https://gitlab.com/api/v4/projects/12345/repository/files/my%2Fpath%20with%20spaces%2Fto%2Ffile.yml/raw?ref=branch',
},
// Raw URLs
{
config: configWithNoToken,
url: 'https://gitlab.example.com/a/b/blob/master/c.yml',
result: 'https://gitlab.example.com/a/b/raw/master/c.yml',
},
])('should handle happy path %#', async ({ config, url, result }) => {
await expect(getGitLabFileFetchUrl(url, config)).resolves.toBe(result);
it('locates projects in subgroups', async () => {
const target =
'https://gitlab.com/group/subgroup/project/-/blob/branch/folder/file.yaml';
const fetchUrl =
'https://gitlab.com/api/v4/projects/12345/repository/files/folder%2Ffile.yaml/raw?ref=branch';
await expect(
getGitLabFileFetchUrl(target, configWithNoToken),
).resolves.toBe(fetchUrl);
});
it('supports filename with .yml extension', async () => {
const target =
'https://gitlab.com/group/project/-/blob/branch/folder/file.yml';
const fetchUrl =
'https://gitlab.com/api/v4/projects/12345/repository/files/folder%2Ffile.yml/raw?ref=branch';
await expect(
getGitLabFileFetchUrl(target, configWithNoToken),
).resolves.toBe(fetchUrl);
});
it('supports non-URI-encoded target', async () => {
const target =
'https://gitlab.com/group/project/-/blob/branch/folder/file with spaces.yaml';
const fetchUrl =
'https://gitlab.com/api/v4/projects/12345/repository/files/folder%2Ffile%20with%20spaces.yaml/raw?ref=branch';
await expect(
getGitLabFileFetchUrl(target, configWithNoToken),
).resolves.toBe(fetchUrl);
});
describe('when gitlab is self-hosted', () => {
it('returns projects API URL', async () => {
const target =
'https://gitlab.mycompany.com/group/project/-/blob/branch/folder/file.yaml';
const fetchUrl =
'https://gitlab.mycompany.com/api/v4/projects/12345/repository/files/folder%2Ffile.yaml/raw?ref=branch';
await expect(
getGitLabFileFetchUrl(target, configSelfHostedWithoutRelativePath),
).resolves.toBe(fetchUrl);
});
it('handles non-URI-encoded target', async () => {
const target =
'https://gitlab.mycompany.com/group/project/-/blob/branch/folder/file with spaces.yaml';
const fetchUrl =
'https://gitlab.mycompany.com/api/v4/projects/12345/repository/files/folder%2Ffile%20with%20spaces.yaml/raw?ref=branch';
await expect(
getGitLabFileFetchUrl(target, configSelfHostedWithoutRelativePath),
).resolves.toBe(fetchUrl);
});
describe('with a relative path', () => {
it('returns projects API URL', async () => {
const target =
'https://gitlab.mycompany.com/gitlab/group/project/-/blob/branch/folder/file.yaml';
const fetchUrl =
'https://gitlab.mycompany.com/gitlab/api/v4/projects/12345/repository/files/folder%2Ffile.yaml/raw?ref=branch';
await expect(
getGitLabFileFetchUrl(target, configSelfHosteWithRelativePath),
).resolves.toBe(fetchUrl);
});
it('handles non-URI-encoded target', async () => {
const target =
'https://gitlab.mycompany.com/gitlab/group/project/-/blob/branch/folder/file with spaces.yaml';
const fetchUrl =
'https://gitlab.mycompany.com/gitlab/api/v4/projects/12345/repository/files/folder%2Ffile%20with%20spaces.yaml/raw?ref=branch';
await expect(
getGitLabFileFetchUrl(target, configSelfHosteWithRelativePath),
).resolves.toBe(fetchUrl);
});
});
});
});
describe('when target has an unscoped route', () => {
it('returns a raw URL', async () => {
const target =
'https://gitlab.com/group/project/blob/branch/folder/file.yaml';
const fetchUrl =
'https://gitlab.com/group/project/raw/branch/folder/file.yaml';
await expect(
getGitLabFileFetchUrl(target, configWithNoToken),
).resolves.toBe(fetchUrl);
});
it('supports project in subgroup', async () => {
const target =
'https://gitlab.com/group/subgroup/project/blob/branch/folder/file.yaml';
const fetchUrl =
'https://gitlab.com/group/subgroup/project/raw/branch/folder/file.yaml';
await expect(
getGitLabFileFetchUrl(target, configWithNoToken),
).resolves.toBe(fetchUrl);
});
it('supports project named "blob"', async () => {
const target =
'https://gitlab.com/group/blob/blob/branch/folder/file.yaml';
const fetchUrl =
'https://gitlab.com/group/blob/raw/branch/folder/file.yaml';
await expect(
getGitLabFileFetchUrl(target, configWithNoToken),
).resolves.toBe(fetchUrl);
});
it('supports repo with branch named "blob"', async () => {
const target =
'https://gitlab.com/group/project/blob/blob/folder/file.yaml';
const fetchUrl =
'https://gitlab.com/group/project/raw/blob/folder/file.yaml';
await expect(
getGitLabFileFetchUrl(target, configWithNoToken),
).resolves.toBe(fetchUrl);
});
it('removes empty path segment', async () => {
const target = 'https://gitlab.example.com/group//blob/blob/file.yaml';
const fetchUrl = 'https://gitlab.example.com/group/blob/raw/file.yaml';
await expect(
getGitLabFileFetchUrl(target, configWithNoToken),
).resolves.toBe(fetchUrl);
});
});
});
});