Skip to content

Commit

Permalink
fix(amplify-cli):Moved function to seperate file
Browse files Browse the repository at this point in the history
  • Loading branch information
studpeps committed Jul 17, 2021
1 parent e3b99c0 commit facd0a7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
16 changes: 4 additions & 12 deletions packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import execa from 'execa';
import * as fs from 'fs-extra';
import { hideSync } from 'hidefile';
import { setRegPendingDelete } from '../../utils/win-utils';
import { snapshotSerializer } from '../../snapshot-serializer';
import path from 'path';

jest.mock('execa');
const execa_mock = execa as jest.Mocked<typeof execa>;
Expand Down Expand Up @@ -43,6 +45,7 @@ const setRegPendingDelete_mock = setRegPendingDelete as jest.MockedFunction<type

// save original process.platform
const originalPlatform = process.platform;
snapshotSerializer();

const context_stub_typed = (context_stub as unknown) as $TSContext;

Expand All @@ -61,17 +64,6 @@ describe('uninstall packaged CLI on mac / linux', () => {
});
});

expect.addSnapshotSerializer({
test(val) {
return typeof val === 'string';
},
print(val) {
if (typeof val === 'string')
return `"${val.replace(/\\/ig, '/')}"`;
return '';
},
});

it('removes the .amplify dir', async () => {
await run(context_stub_typed);

Expand Down Expand Up @@ -118,7 +110,7 @@ describe('uninstall packaged CLI on windows', () => {

expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"homedir/.amplify/bin/amplify.exe",
"${path.join('homedir','.amplify','bin','amplify.exe')}",
"a/test/path/.amplify-pending-delete.exe",
Object {
"overwrite": true,
Expand Down
29 changes: 10 additions & 19 deletions packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fetch, { Response } from 'node-fetch';
import { $TSContext } from 'amplify-cli-core';
import * as core from 'amplify-cli-core';
import * as path from 'path';
import {snapshotSerializer} from '../../snapshot-serializer';
jest.mock('fs-extra');
const fs_mock = (fs as unknown) as jest.Mocked<typeof fs>;

Expand Down Expand Up @@ -45,6 +46,7 @@ const context_stub_typed = (context_stub as unknown) as $TSContext;

// save original process.platform
const originalPlatform = process.platform;
snapshotSerializer();

describe('run upgrade using packaged CLI', () => {
beforeEach(() => jest.clearAllMocks());
Expand All @@ -53,17 +55,6 @@ describe('run upgrade using packaged CLI', () => {
value: originalPlatform,
});
});

expect.addSnapshotSerializer({
test(val) {
return typeof val === 'string';
},
print(val) {
if (typeof val === 'string')
return `"${val.replace(/\\/ig, '/')}"`;
return '';
},
});

it('exits early if no new packaged version available', async () => {
// setup
Expand Down Expand Up @@ -109,8 +100,8 @@ describe('run upgrade using packaged CLI', () => {
// validate
expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"${path.posix.join('homedir', 'bin', 'amplify-pkg-linux')}",
"${path.posix.join('homedir', 'bin', 'amplify')}",
"${path.join('homedir', 'bin', 'amplify-pkg-linux')}",
"${path.join('homedir', 'bin', 'amplify')}",
Object {
"overwrite": true,
},
Expand All @@ -119,7 +110,7 @@ describe('run upgrade using packaged CLI', () => {

expect(fs_mock.chmod.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"homedir/bin/amplify",
"${path.join('homedir', 'bin', 'amplify')}",
"700",
]
`);
Expand Down Expand Up @@ -161,22 +152,22 @@ describe('run upgrade using packaged CLI', () => {
// validate
expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"${path.posix.join('homedir', 'bin', 'amplify.exe')}",
"${path.posix.join('homedir', 'bin', 'amplify-old.exe')}",
"${path.join('homedir', 'bin', 'amplify.exe')}",
"${path.join('homedir', 'bin', 'amplify-old.exe')}",
]
`);
expect(fs_mock.move.mock.calls[1]).toMatchInlineSnapshot(`
Array [
"${path.posix.join('homedir', 'bin', 'amplify-pkg-win.exe')}",
"${path.posix.join('homedir', 'bin', 'amplify.exe')}",
"${path.join('homedir', 'bin', 'amplify-pkg-win.exe')}",
"${path.join('homedir', 'bin', 'amplify.exe')}",
Object {
"overwrite": true,
},
]
`);
expect(fs_mock.chmod.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"${path.posix.join('homedir', 'bin', 'amplify.exe')}",
"${path.join('homedir', 'bin', 'amplify.exe')}",
"700",
]
`);
Expand Down
12 changes: 12 additions & 0 deletions packages/amplify-cli/src/snapshot-serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const originalPlatform = process.platform;
// converts inputs to toMatchInlineSnapshot() function into supported format
export function snapshotSerializer(){
expect.addSnapshotSerializer({
test(val) {
return typeof val === 'string' && originalPlatform === 'win32';
},
print(val) {
return `"${(val as string).replace(/\\/g, '\\')}"`;
},
});
}

0 comments on commit facd0a7

Please sign in to comment.