Use windows friendly temp dir

This commit is contained in:
Johan Haals
2021-01-27 16:48:19 +01:00
parent 06af096c49
commit 80366b60ad
2 changed files with 28 additions and 21 deletions
@@ -15,17 +15,18 @@
*/
import fs from 'fs-extra';
import os from 'os';
import { resolve } from 'path';
import { GithubPreparer } from './github';
import {
TemplateEntityV1alpha1,
LOCATION_ANNOTATION,
} from '@backstage/catalog-model';
import { getVoidLogger, Git } from '@backstage/backend-common';
jest.mock('fs-extra');
describe('GitHubPreparer', () => {
let mockEntity: TemplateEntityV1alpha1;
const workspacePath = os.platform() === 'win32' ? 'C:\\tmp' : '/tmp';
const checkoutPath = resolve(workspacePath, 'checkout');
const templatePath = resolve(workspacePath, 'template');
const mockGitClient = {
clone: jest.fn(),
};
@@ -47,16 +48,16 @@ describe('GitHubPreparer', () => {
url:
'https://github.com/benjdlambert/backstage-graphql-template/blob/master/templates/graphql-starter/template',
logger,
workspacePath: '/tmp',
workspacePath,
});
expect(mockGitClient.clone).toHaveBeenCalledWith({
url: 'https://github.com/benjdlambert/backstage-graphql-template',
dir: expect.any(String),
dir: checkoutPath,
});
expect(fs.move).toHaveBeenCalledWith(
'/tmp/checkout/templates/graphql-starter/template',
'/tmp/template',
resolve(checkoutPath, 'templates', 'graphql-starter', 'template'),
templatePath,
);
expect(fs.rmdir).toHaveBeenCalledWith('/tmp/template/.git');
});
@@ -66,15 +67,15 @@ describe('GitHubPreparer', () => {
url:
'https://github.com/benjdlambert/backstage-graphql-template/blob/master',
logger,
workspacePath: '/tmp',
workspacePath,
});
expect(mockGitClient.clone).toHaveBeenCalledWith({
url: 'https://github.com/benjdlambert/backstage-graphql-template',
dir: expect.any(String),
dir: checkoutPath,
});
expect(fs.move).toHaveBeenCalledWith('/tmp/checkout', '/tmp/template');
expect(fs.rmdir).toHaveBeenCalledWith('/tmp/template/.git');
expect(fs.move).toHaveBeenCalledWith(checkoutPath, templatePath);
expect(fs.rmdir).toHaveBeenCalledWith(resolve(templatePath, '.git'));
});
it('calls the clone command with token', async () => {
@@ -82,7 +83,7 @@ describe('GitHubPreparer', () => {
url:
'https://github.com/benjdlambert/backstage-graphql-template/blob/master',
logger,
workspacePath: '/tmp',
workspacePath,
});
expect(Git.fromAuth).toHaveBeenCalledWith({
@@ -14,12 +14,18 @@
* limitations under the License.
*/
import fs from 'fs-extra';
import os from 'os';
import { resolve } from 'path';
import { GitlabPreparer } from './gitlab';
import { getVoidLogger, Git } from '@backstage/backend-common';
jest.mock('fs-extra');
describe('GitLabPreparer', () => {
const workspacePath = os.platform() === 'win32' ? 'C:\\tmp' : '/tmp';
const checkoutPath = resolve(workspacePath, 'checkout');
const templatePath = resolve(workspacePath, 'template');
const mockGitClient = {
clone: jest.fn(),
};
@@ -40,12 +46,12 @@ describe('GitLabPreparer', () => {
url:
'https://gitlab.com/benjdlambert/backstage-graphql-template/-/blob/master',
logger,
workspacePath: '/tmp',
workspacePath,
});
expect(mockGitClient.clone).toHaveBeenCalledWith({
url: 'https://gitlab.com/benjdlambert/backstage-graphql-template',
dir: '/tmp/checkout',
dir: checkoutPath,
});
expect(Git.fromAuth).toHaveBeenCalledWith({
@@ -54,8 +60,8 @@ describe('GitLabPreparer', () => {
password: 'fake-token',
});
expect(fs.move).toHaveBeenCalledWith('/tmp/checkout', '/tmp/template');
expect(fs.rmdir).toHaveBeenCalledWith('/tmp/template/.git');
expect(fs.move).toHaveBeenCalledWith(checkoutPath, templatePath);
expect(fs.rmdir).toHaveBeenCalledWith(resolve(templatePath, '.git'));
});
it(`clones the template from a sub directory if specified`, async () => {
@@ -63,11 +69,11 @@ describe('GitLabPreparer', () => {
url:
'https://gitlab.com/benjdlambert/backstage-graphql-template/-/blob/master/1/2/3',
logger,
workspacePath: '/tmp',
workspacePath,
});
expect(fs.move).toHaveBeenCalledWith(
'/tmp/checkout/1/2/3',
'/tmp/template',
resolve(checkoutPath, '1', '2', '3'),
templatePath,
);
});
});