Scaffolder: Fix tests in windows

This commit is contained in:
Johan Haals
2021-01-29 13:42:53 +01:00
parent 2bc408228b
commit 0cb0715700
7 changed files with 72 additions and 44 deletions
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import os from 'os';
import { JobProcessor } from './processor';
import { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
import { StageInput } from './types';
@@ -68,9 +69,11 @@ describe('JobProcessor', () => {
},
};
const workingDirectory = os.platform() === 'win32' ? 'C:\\tmp' : '/tmp';
describe('create', () => {
it('creates should create a new job with a unique id', async () => {
const processor = new JobProcessor('/tmp');
const processor = new JobProcessor(workingDirectory);
const job = processor.create({
entity: mockEntity,
@@ -84,7 +87,7 @@ describe('JobProcessor', () => {
});
it('should setup the correct context for the job', async () => {
const processor = new JobProcessor('/tmp');
const processor = new JobProcessor(workingDirectory);
const job = processor.create({
entity: mockEntity,
@@ -97,7 +100,7 @@ describe('JobProcessor', () => {
});
it('should set the status as pending', async () => {
const processor = new JobProcessor('/tmp');
const processor = new JobProcessor(workingDirectory);
const job = processor.create({
entity: mockEntity,
@@ -120,7 +123,7 @@ describe('JobProcessor', () => {
},
];
const processor = new JobProcessor('/tmp');
const processor = new JobProcessor(workingDirectory);
const job = processor.create({
entity: mockEntity,
@@ -139,12 +142,12 @@ describe('JobProcessor', () => {
describe('get', () => {
it('return undefined for when the job does not exist', () => {
const processor = new JobProcessor('/tmp');
const processor = new JobProcessor(workingDirectory);
expect(processor.get('123')).not.toBeDefined();
});
it('should return the exact same instance of the job when one is created', async () => {
const processor = new JobProcessor('/tmp');
const processor = new JobProcessor(workingDirectory);
const job = processor.create({
entity: mockEntity,
values: mockValues,
@@ -156,7 +159,7 @@ describe('JobProcessor', () => {
});
describe('process', () => {
it('throws an error when the status of the job is not in pending state', async () => {
const processor = new JobProcessor('/tmp');
const processor = new JobProcessor(workingDirectory);
const job = processor.create({
entity: mockEntity,
values: mockValues,
@@ -182,7 +185,7 @@ describe('JobProcessor', () => {
},
];
const processor = new JobProcessor('/tmp');
const processor = new JobProcessor(workingDirectory);
const job = processor.create({
entity: mockEntity,
values: mockValues,
@@ -208,7 +211,7 @@ describe('JobProcessor', () => {
},
];
const processor = new JobProcessor('/tmp');
const processor = new JobProcessor(workingDirectory);
const job = processor.create({
entity: mockEntity,
values: mockValues,
@@ -244,7 +247,7 @@ describe('JobProcessor', () => {
},
];
const processor = new JobProcessor('/tmp');
const processor = new JobProcessor(workingDirectory);
const job = processor.create({
entity: mockEntity,
values: mockValues,
@@ -283,7 +286,7 @@ describe('JobProcessor', () => {
},
];
const processor = new JobProcessor('/tmp');
const processor = new JobProcessor(workingDirectory);
const job = processor.create({
entity: mockEntity,
values: mockValues,
@@ -18,7 +18,7 @@ import { getVoidLogger } from '@backstage/backend-common';
import fs from 'fs-extra';
import { FilePreparer } from './file';
import os from 'os';
import { resolve } from 'path';
import path, { resolve } from 'path';
jest.mock('fs-extra');
@@ -35,7 +35,7 @@ describe('File preparer', () => {
workspacePath,
});
expect(fs.copy).toHaveBeenCalledWith(
resolve('/path', 'to', 'template'),
path.join('/path', 'to', 'template'),
checkoutPath,
{
recursive: true,
@@ -20,6 +20,8 @@ jest.mock('azure-devops-node-api', () => ({
getPersonalAccessTokenHandler: jest.fn().mockReturnValue(() => {}),
}));
import os from 'os';
import { resolve } from 'path';
import { AzurePublisher } from './azure';
import { WebApi } from 'azure-devops-node-api';
import * as helpers from './helpers';
@@ -28,6 +30,9 @@ import { getVoidLogger } from '@backstage/backend-common';
describe('Azure Publisher', () => {
const logger = getVoidLogger();
const workspacePath = os.platform() === 'win32' ? 'C:\\tmp' : '/tmp';
const resultPath = resolve(workspacePath, 'result');
describe('publish: createRemoteInAzure', () => {
it('should use azure-devops-node-api to create a repo in the given project', async () => {
const mockGitClient = {
@@ -53,7 +58,7 @@ describe('Azure Publisher', () => {
storePath: 'https://dev.azure.com/organisation/project/_git/repo',
owner: 'bob',
},
workspacePath: '/tmp/test',
workspacePath,
logger,
});
@@ -74,7 +79,7 @@ describe('Azure Publisher', () => {
'project',
);
expect(helpers.initRepoAndPush).toHaveBeenCalledWith({
dir: '/tmp/test/result',
dir: resultPath,
remoteUrl: 'https://dev.azure.com/organization/project/_git/repo',
auth: { username: 'notempty', password: 'fake-azure-token' },
logger,
@@ -16,6 +16,8 @@
jest.mock('./helpers');
import os from 'os';
import { resolve } from 'path';
import { BitbucketPublisher } from './bitbucket';
import { initRepoAndPush } from './helpers';
import { getVoidLogger } from '@backstage/backend-common';
@@ -32,6 +34,9 @@ describe('Bitbucket Publisher', () => {
jest.clearAllMocks();
});
const workspacePath = os.platform() === 'win32' ? 'C:\\tmp' : '/tmp';
const resultPath = resolve(workspacePath, 'result');
describe('publish: createRemoteInBitbucketCloud', () => {
it('should create repo in bitbucket cloud', async () => {
server.use(
@@ -69,7 +74,7 @@ describe('Bitbucket Publisher', () => {
storePath: 'https://bitbucket.org/project/repo',
owner: 'bob',
},
workspacePath: '/tmp/test',
workspacePath,
logger: logger,
});
@@ -80,7 +85,7 @@ describe('Bitbucket Publisher', () => {
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: '/tmp/test/result',
dir: resultPath,
remoteUrl: 'https://bitbucket.org/project/repo',
auth: { username: 'fake-user', password: 'fake-token' },
logger: logger,
@@ -127,7 +132,7 @@ describe('Bitbucket Publisher', () => {
storePath: 'https://bitbucket.mycompany.com/project/repo',
owner: 'bob',
},
workspacePath: '/tmp/test',
workspacePath,
logger: logger,
});
@@ -138,7 +143,7 @@ describe('Bitbucket Publisher', () => {
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: '/tmp/test/result',
dir: resultPath,
remoteUrl: 'https://bitbucket.mycompany.com/scm/project/repo',
auth: { username: 'x-token-auth', password: 'fake-token' },
logger: logger,
@@ -17,6 +17,9 @@
jest.mock('@octokit/rest');
jest.mock('./helpers');
import os from 'os';
import { resolve } from 'path';
import { getVoidLogger } from '@backstage/backend-common';
import { Octokit, RestEndpointMethodTypes } from '@octokit/rest';
import { GithubPublisher } from './github';
@@ -36,6 +39,9 @@ describe('GitHub Publisher', () => {
jest.clearAllMocks();
});
const workspacePath = os.platform() === 'win32' ? 'C:\\tmp' : '/tmp';
const resultPath = resolve(workspacePath, 'result');
describe('with public repo visibility', () => {
describe('publish: createRemoteInGithub', () => {
it('should use octokit to create a repo in an organisation if the organisation property is set', async () => {
@@ -64,7 +70,7 @@ describe('GitHub Publisher', () => {
owner: 'bob',
access: 'blam/team',
},
workspacePath: '/tmp/test',
workspacePath,
logger,
});
@@ -89,7 +95,7 @@ describe('GitHub Publisher', () => {
permission: 'admin',
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: '/tmp/test/result',
dir: resultPath,
remoteUrl: 'https://github.com/backstage/backstage.git',
auth: { username: 'fake-token', password: 'x-oauth-basic' },
logger,
@@ -122,7 +128,7 @@ describe('GitHub Publisher', () => {
owner: 'bob',
access: 'blam',
},
workspacePath: '/tmp/test',
workspacePath,
logger,
});
@@ -140,7 +146,7 @@ describe('GitHub Publisher', () => {
expect(mockGithubClient.repos.addCollaborator).not.toHaveBeenCalled();
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: '/tmp/test/result',
dir: resultPath,
remoteUrl: 'https://github.com/backstage/backstage.git',
auth: { username: 'fake-token', password: 'x-oauth-basic' },
logger,
@@ -175,7 +181,7 @@ describe('GitHub Publisher', () => {
access: 'bob',
description: 'description',
},
workspacePath: '/tmp/test',
workspacePath,
logger,
});
@@ -198,7 +204,7 @@ describe('GitHub Publisher', () => {
permission: 'admin',
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: '/tmp/test/result',
dir: resultPath,
remoteUrl: 'https://github.com/backstage/backstage.git',
auth: { username: 'fake-token', password: 'x-oauth-basic' },
logger,
@@ -233,7 +239,7 @@ describe('GitHub Publisher', () => {
storePath: 'https://github.com/blam/test',
owner: 'bob',
},
workspacePath: '/tmp/test',
workspacePath,
logger,
});
@@ -249,7 +255,7 @@ describe('GitHub Publisher', () => {
visibility: 'internal',
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: '/tmp/test/result',
dir: resultPath,
remoteUrl: 'https://github.com/backstage/backstage.git',
auth: { username: 'fake-token', password: 'x-oauth-basic' },
logger,
@@ -283,7 +289,7 @@ describe('GitHub Publisher', () => {
storePath: 'https://github.com/blam/test',
owner: 'bob',
},
workspacePath: '/tmp/test',
workspacePath,
logger,
});
@@ -299,7 +305,7 @@ describe('GitHub Publisher', () => {
private: true,
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: '/tmp/test/result',
dir: resultPath,
remoteUrl: 'https://github.com/backstage/backstage.git',
auth: { username: 'fake-token', password: 'x-oauth-basic' },
logger,
@@ -20,6 +20,8 @@ jest.mock('@gitbeaker/node', () => ({
jest.mock('./helpers');
import os from 'os';
import { resolve } from 'path';
import { GitlabPublisher } from './gitlab';
import { Gitlab } from '@gitbeaker/node';
import { initRepoAndPush } from './helpers';
@@ -47,6 +49,9 @@ describe('GitLab Publisher', () => {
);
});
const workspacePath = os.platform() === 'win32' ? 'C:\\tmp' : '/tmp';
const resultPath = resolve(workspacePath, 'result');
describe('publish: createRemoteInGitLab', () => {
it('should use gitbeaker to create a repo in a namespace if the namespace property is set', async () => {
const publisher = await GitlabPublisher.fromConfig({
@@ -68,7 +73,7 @@ describe('GitLab Publisher', () => {
storePath: 'https://gitlab.com/blam/test',
owner: 'bob',
},
workspacePath: '/tmp/test',
workspacePath,
logger,
});
@@ -85,7 +90,7 @@ describe('GitLab Publisher', () => {
name: 'test',
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: '/tmp/test/result',
dir: resultPath,
remoteUrl: 'mockclone',
auth: { username: 'oauth2', password: 'fake-token' },
logger,
@@ -111,7 +116,7 @@ describe('GitLab Publisher', () => {
storePath: 'https://gitlab.com/blam/test',
owner: 'bob',
},
workspacePath: '/tmp/test',
workspacePath,
logger,
});
@@ -125,7 +130,7 @@ describe('GitLab Publisher', () => {
name: 'test',
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: '/tmp/test/result',
dir: resultPath,
remoteUrl: 'mockclone',
auth: { username: 'oauth2', password: 'fake-token' },
logger,
@@ -24,6 +24,7 @@ jest.mock('fs-extra');
import { CookieCutter } from './cookiecutter';
import fs from 'fs-extra';
import path from 'path';
import { PassThrough } from 'stream';
import Docker from 'dockerode';
import parseGitUrl from 'git-url-parse';
@@ -56,7 +57,7 @@ describe('CookieCutter Templater', () => {
});
expect(fs.writeJson).toBeCalledWith(
'tempdir/template/cookiecutter.json',
path.join('tempdir', 'template', 'cookiecutter.json'),
expect.objectContaining(values),
);
});
@@ -87,13 +88,16 @@ describe('CookieCutter Templater', () => {
dockerClient: mockDocker,
});
expect(fs.writeJSON).toBeCalledWith('tempdir/template/cookiecutter.json', {
...existingJson,
...values,
destination: {
git: expect.objectContaining({ organization: 'org', name: 'repo' }),
expect(fs.writeJSON).toBeCalledWith(
path.join('tempdir', 'template', 'cookiecutter.json'),
{
...existingJson,
...values,
destination: {
git: expect.objectContaining({ organization: 'org', name: 'repo' }),
},
},
});
);
});
it('should throw an error if the cookiecutter json is malformed and not missing', async () => {
@@ -148,8 +152,8 @@ describe('CookieCutter Templater', () => {
'/template',
'--verbose',
],
templateDir: 'tempdir/template',
resultDir: 'tempdir/intermediate',
templateDir: path.join('tempdir', 'template'),
resultDir: path.join('tempdir', 'intermediate'),
logStream: undefined,
dockerClient: mockDocker,
});
@@ -187,8 +191,8 @@ describe('CookieCutter Templater', () => {
'/template',
'--verbose',
],
templateDir: 'tempdir/template',
resultDir: 'tempdir/intermediate',
templateDir: path.join('tempdir', 'template'),
resultDir: path.join('tempdir', 'intermediate'),
logStream: stream,
dockerClient: mockDocker,
});