From 3a4826b17902e5894178014d514cb1815742b15b Mon Sep 17 00:00:00 2001 From: studpeps Date: Tue, 13 Jul 2021 13:54:21 +0530 Subject: [PATCH 01/10] bug(amplify-cli):Solved Windows compatibility issue, made location paths os-specific --- .../__tests__/commands/uninstall.pkg.test.ts | 8 ++- .../__tests__/commands/upgrade.pkg.test.ts | 53 ++++++++++++++----- .../amplify-helpers/remove-resource.test.ts | 4 +- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts b/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts index f76f05c4154..ecfc43aa8db 100644 --- a/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts +++ b/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts @@ -4,7 +4,9 @@ import execa from 'execa'; import * as fs from 'fs-extra'; import { hideSync } from 'hidefile'; import { setRegPendingDelete } from '../../utils/win-utils'; +import * as path from 'path'; +const rex1 = /\\/ig; jest.mock('execa'); const execa_mock = execa as jest.Mocked; execa_mock.command.mockResolvedValue({} as any); @@ -104,9 +106,13 @@ describe('uninstall packaged CLI on windows', () => { await run(context_stub_typed); + if (typeof fs_mock.move.mock.calls[0][0] === 'string') { + fs_mock.move.mock.calls[0][0] = fs_mock.move.mock.calls[0][0].replace(rex1, '/'); + } + expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "homedir/.amplify/bin/amplify.exe", + "${path.posix.join('homedir','.amplify','bin','amplify.exe')}", "a/test/path/.amplify-pending-delete.exe", Object { "overwrite": true, diff --git a/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts b/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts index 1df2362c221..2119a8a52b2 100644 --- a/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts +++ b/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts @@ -3,9 +3,11 @@ import { run } from '../../commands/upgrade'; import fetch, { Response } from 'node-fetch'; import { $TSContext } from 'amplify-cli-core'; import * as core from 'amplify-cli-core'; - +import * as path from "path"; jest.mock('fs-extra'); const fs_mock = (fs as unknown) as jest.Mocked; +const rex1 = /\\/ig; + jest.mock('node-fetch'); const fetch_mock = fetch as jest.MockedFunction; @@ -54,6 +56,27 @@ describe('run upgrade using packaged CLI', () => { }); }); + async function replaceSlashes() { + for (let i = 0; i < fs_mock.move.mock.calls[0].length; i++) { + if (typeof fs_mock.move.mock.calls[0]['' + i] === 'string') { + fs_mock.move.mock.calls[0]['' + i] = fs_mock.move.mock.calls[0]['' + i].replace(rex1, '/'); + } + } + + for (let i = 0; i < fs_mock.move.mock.calls[1].length; i++) { + if (typeof fs_mock.move.mock.calls[1]['' + i] === 'string') { + fs_mock.move.mock.calls[1]['' + i] = fs_mock.move.mock.calls[1]['' + i].replace(rex1, '/'); + } + } + + for (let i = 0; i < fs_mock.chmod.mock.calls[0].length; i++) { + if (typeof fs_mock.chmod.mock.calls[0][0] === 'string') { + fs_mock.chmod.mock.calls[0][0] = fs_mock.chmod.mock.calls[0][0].replace(rex1, '/'); + } + } + } + + it('exits early if no new packaged version available', async () => { // setup fetch_mock.mockResolvedValueOnce(({ @@ -96,18 +119,23 @@ describe('run upgrade using packaged CLI', () => { await run(context_stub_typed); // validate + replaceSlashes(); expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "homedir/bin/amplify-pkg-linux", - "homedir/bin/amplify", + "${path.posix.join('homedir', 'bin', 'amplify-pkg-linux')}", + "${path.posix.join('homedir', 'bin', 'amplify')}", Object { "overwrite": true, }, ] `); + + if (typeof fs_mock.chmod.mock.calls[0][0] === 'string') { + fs_mock.chmod.mock.calls[0][0] = fs_mock.chmod.mock.calls[0][0].replace(rex1, '/'); + } expect(fs_mock.chmod.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "homedir/bin/amplify", + "${path.posix.join('homedir', 'bin', 'amplify')}", "700", ] `); @@ -147,24 +175,25 @@ describe('run upgrade using packaged CLI', () => { await run(context_stub_typed); // validate - expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(` + replaceSlashes(); + expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "homedir/bin/amplify.exe", - "homedir/bin/amplify-old.exe", + "${path.posix.join('homedir', 'bin', 'amplify.exe')}", + "${path.posix.join('homedir', 'bin', 'amplify-old.exe')}", ] `); - expect(fs_mock.move.mock.calls[1]).toMatchInlineSnapshot(` + expect(fs_mock.move.mock.calls[1]).toMatchInlineSnapshot(` Array [ - "homedir/bin/amplify-pkg-win.exe", - "homedir/bin/amplify.exe", + "${path.posix.join('homedir', 'bin', 'amplify-pkg-win.exe')}", + "${path.posix.join('homedir', 'bin', 'amplify.exe')}", Object { "overwrite": true, }, ] `); - expect(fs_mock.chmod.mock.calls[0]).toMatchInlineSnapshot(` + expect(fs_mock.chmod.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "homedir/bin/amplify.exe", + "${path.posix.join('homedir', 'bin', 'amplify.exe')}", "700", ] `); diff --git a/packages/amplify-cli/src/__tests__/extensions/amplify-helpers/remove-resource.test.ts b/packages/amplify-cli/src/__tests__/extensions/amplify-helpers/remove-resource.test.ts index 072a4765865..26fcfc16bdc 100644 --- a/packages/amplify-cli/src/__tests__/extensions/amplify-helpers/remove-resource.test.ts +++ b/packages/amplify-cli/src/__tests__/extensions/amplify-helpers/remove-resource.test.ts @@ -3,7 +3,7 @@ import { stateManager, exitOnNextTick, ResourceDoesNotExistError, MissingParamet import * as inquirer from 'inquirer'; import { updateBackendConfigAfterResourceRemove } from '../../../extensions/amplify-helpers/update-backend-config'; import { removeResourceParameters } from '../../../extensions/amplify-helpers/envResourceParams'; - +const path = require('path'); jest.mock('../../../extensions/amplify-helpers/envResourceParams'); jest.mock('../../../extensions/amplify-helpers/update-backend-config'); @@ -215,7 +215,7 @@ describe('remove-resource', () => { }, }, }); - expect(context.filesystem.remove).toBeCalledWith('backendDirPath/function/lambda1'); + expect(context.filesystem.remove).toBeCalledWith(path.join("backendDirPath", "function","lambda1")); expect(removeResourceParameters).toBeCalledWith(context, 'function', 'lambda1'); expect(updateBackendConfigAfterResourceRemove).toBeCalledWith('function', 'lambda1'); expect(context.print.success).toBeCalledWith('Successfully removed resource'); From 54a556bcdef3911f687463ecfcb7ef35add837ed Mon Sep 17 00:00:00 2001 From: Stuti Prasad <43947328+studpeps@users.noreply.github.com> Date: Tue, 13 Jul 2021 14:01:14 +0530 Subject: [PATCH 02/10] Removed resource-test updation --- .../extensions/amplify-helpers/remove-resource.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/amplify-cli/src/__tests__/extensions/amplify-helpers/remove-resource.test.ts b/packages/amplify-cli/src/__tests__/extensions/amplify-helpers/remove-resource.test.ts index 26fcfc16bdc..072a4765865 100644 --- a/packages/amplify-cli/src/__tests__/extensions/amplify-helpers/remove-resource.test.ts +++ b/packages/amplify-cli/src/__tests__/extensions/amplify-helpers/remove-resource.test.ts @@ -3,7 +3,7 @@ import { stateManager, exitOnNextTick, ResourceDoesNotExistError, MissingParamet import * as inquirer from 'inquirer'; import { updateBackendConfigAfterResourceRemove } from '../../../extensions/amplify-helpers/update-backend-config'; import { removeResourceParameters } from '../../../extensions/amplify-helpers/envResourceParams'; -const path = require('path'); + jest.mock('../../../extensions/amplify-helpers/envResourceParams'); jest.mock('../../../extensions/amplify-helpers/update-backend-config'); @@ -215,7 +215,7 @@ describe('remove-resource', () => { }, }, }); - expect(context.filesystem.remove).toBeCalledWith(path.join("backendDirPath", "function","lambda1")); + expect(context.filesystem.remove).toBeCalledWith('backendDirPath/function/lambda1'); expect(removeResourceParameters).toBeCalledWith(context, 'function', 'lambda1'); expect(updateBackendConfigAfterResourceRemove).toBeCalledWith('function', 'lambda1'); expect(context.print.success).toBeCalledWith('Successfully removed resource'); From 779a4185825276a057982ea3339158285c637c83 Mon Sep 17 00:00:00 2001 From: studpeps Date: Wed, 14 Jul 2021 21:23:01 +0530 Subject: [PATCH 03/10] bug(amplify-cli):added addSnapshotSerializer functionality --- .../__tests__/commands/uninstall.pkg.test.ts | 20 +++++---- .../__tests__/commands/upgrade.pkg.test.ts | 41 ++++++------------- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts b/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts index ecfc43aa8db..804f17b258f 100644 --- a/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts +++ b/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts @@ -4,9 +4,7 @@ import execa from 'execa'; import * as fs from 'fs-extra'; import { hideSync } from 'hidefile'; import { setRegPendingDelete } from '../../utils/win-utils'; -import * as path from 'path'; -const rex1 = /\\/ig; jest.mock('execa'); const execa_mock = execa as jest.Mocked; execa_mock.command.mockResolvedValue({} as any); @@ -62,6 +60,18 @@ describe('uninstall packaged CLI on mac / linux', () => { value: originalPlatform, }); }); + + 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); @@ -106,13 +116,9 @@ describe('uninstall packaged CLI on windows', () => { await run(context_stub_typed); - if (typeof fs_mock.move.mock.calls[0][0] === 'string') { - fs_mock.move.mock.calls[0][0] = fs_mock.move.mock.calls[0][0].replace(rex1, '/'); - } - expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "${path.posix.join('homedir','.amplify','bin','amplify.exe')}", + "homedir/.amplify/bin/amplify.exe", "a/test/path/.amplify-pending-delete.exe", Object { "overwrite": true, diff --git a/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts b/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts index 2119a8a52b2..043d891d182 100644 --- a/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts +++ b/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts @@ -6,8 +6,6 @@ import * as core from 'amplify-cli-core'; import * as path from "path"; jest.mock('fs-extra'); const fs_mock = (fs as unknown) as jest.Mocked; -const rex1 = /\\/ig; - jest.mock('node-fetch'); const fetch_mock = fetch as jest.MockedFunction; @@ -56,27 +54,17 @@ describe('run upgrade using packaged CLI', () => { }); }); - async function replaceSlashes() { - for (let i = 0; i < fs_mock.move.mock.calls[0].length; i++) { - if (typeof fs_mock.move.mock.calls[0]['' + i] === 'string') { - fs_mock.move.mock.calls[0]['' + i] = fs_mock.move.mock.calls[0]['' + i].replace(rex1, '/'); - } - } - - for (let i = 0; i < fs_mock.move.mock.calls[1].length; i++) { - if (typeof fs_mock.move.mock.calls[1]['' + i] === 'string') { - fs_mock.move.mock.calls[1]['' + i] = fs_mock.move.mock.calls[1]['' + i].replace(rex1, '/'); - } - } - - for (let i = 0; i < fs_mock.chmod.mock.calls[0].length; i++) { - if (typeof fs_mock.chmod.mock.calls[0][0] === 'string') { - fs_mock.chmod.mock.calls[0][0] = fs_mock.chmod.mock.calls[0][0].replace(rex1, '/'); - } - } - } - - + 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 fetch_mock.mockResolvedValueOnce(({ @@ -119,7 +107,6 @@ describe('run upgrade using packaged CLI', () => { await run(context_stub_typed); // validate - replaceSlashes(); expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(` Array [ "${path.posix.join('homedir', 'bin', 'amplify-pkg-linux')}", @@ -130,12 +117,9 @@ describe('run upgrade using packaged CLI', () => { ] `); - if (typeof fs_mock.chmod.mock.calls[0][0] === 'string') { - fs_mock.chmod.mock.calls[0][0] = fs_mock.chmod.mock.calls[0][0].replace(rex1, '/'); - } expect(fs_mock.chmod.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "${path.posix.join('homedir', 'bin', 'amplify')}", + "homedir/bin/amplify", "700", ] `); @@ -175,7 +159,6 @@ describe('run upgrade using packaged CLI', () => { await run(context_stub_typed); // validate - replaceSlashes(); expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(` Array [ "${path.posix.join('homedir', 'bin', 'amplify.exe')}", From 3878cd747cd777cda2f93a512893c48fd48e5724 Mon Sep 17 00:00:00 2001 From: Stuti Prasad <43947328+studpeps@users.noreply.github.com> Date: Thu, 15 Jul 2021 12:55:04 +0530 Subject: [PATCH 04/10] Updating "" to '' for standardization Co-authored-by: Colin Ihrig --- packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts b/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts index 043d891d182..64f429f0220 100644 --- a/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts +++ b/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts @@ -3,7 +3,7 @@ import { run } from '../../commands/upgrade'; 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 * as path from 'path'; jest.mock('fs-extra'); const fs_mock = (fs as unknown) as jest.Mocked; From 74d48af7faf23850f63206db595e87828d75438e Mon Sep 17 00:00:00 2001 From: Stuti Prasad <43947328+studpeps@users.noreply.github.com> Date: Thu, 15 Jul 2021 12:59:44 +0530 Subject: [PATCH 05/10] Fixed indentation --- .../amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts b/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts index 64f429f0220..e61562be6e7 100644 --- a/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts +++ b/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts @@ -159,13 +159,13 @@ describe('run upgrade using packaged CLI', () => { await run(context_stub_typed); // validate - expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(` + expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(` Array [ "${path.posix.join('homedir', 'bin', 'amplify.exe')}", "${path.posix.join('homedir', 'bin', 'amplify-old.exe')}", ] `); - expect(fs_mock.move.mock.calls[1]).toMatchInlineSnapshot(` + 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')}", @@ -174,7 +174,7 @@ describe('run upgrade using packaged CLI', () => { }, ] `); - expect(fs_mock.chmod.mock.calls[0]).toMatchInlineSnapshot(` + expect(fs_mock.chmod.mock.calls[0]).toMatchInlineSnapshot(` Array [ "${path.posix.join('homedir', 'bin', 'amplify.exe')}", "700", From facd0a7c6d7cf8e811438bc724042501ed03fed5 Mon Sep 17 00:00:00 2001 From: studpeps Date: Sat, 17 Jul 2021 13:26:35 +0530 Subject: [PATCH 06/10] fix(amplify-cli):Moved function to seperate file --- .../__tests__/commands/uninstall.pkg.test.ts | 16 +++------- .../__tests__/commands/upgrade.pkg.test.ts | 29 +++++++------------ .../amplify-cli/src/snapshot-serializer.ts | 12 ++++++++ 3 files changed, 26 insertions(+), 31 deletions(-) create mode 100644 packages/amplify-cli/src/snapshot-serializer.ts diff --git a/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts b/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts index 804f17b258f..395cc251171 100644 --- a/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts +++ b/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts @@ -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; @@ -43,6 +45,7 @@ const setRegPendingDelete_mock = setRegPendingDelete as jest.MockedFunction { }); }); - 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); @@ -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, diff --git a/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts b/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts index e61562be6e7..9bfadbaebf1 100644 --- a/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts +++ b/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts @@ -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; @@ -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()); @@ -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 @@ -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, }, @@ -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", ] `); @@ -161,14 +152,14 @@ 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, }, @@ -176,7 +167,7 @@ describe('run upgrade using packaged CLI', () => { `); expect(fs_mock.chmod.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "${path.posix.join('homedir', 'bin', 'amplify.exe')}", + "${path.join('homedir', 'bin', 'amplify.exe')}", "700", ] `); diff --git a/packages/amplify-cli/src/snapshot-serializer.ts b/packages/amplify-cli/src/snapshot-serializer.ts new file mode 100644 index 00000000000..ffdbcbf7484 --- /dev/null +++ b/packages/amplify-cli/src/snapshot-serializer.ts @@ -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, '\\')}"`; + }, + }); +} \ No newline at end of file From 1581f2d3e6b8e94a7f57da8915540a86253d2aa3 Mon Sep 17 00:00:00 2001 From: studpeps Date: Wed, 21 Jul 2021 00:14:24 +0530 Subject: [PATCH 07/10] bug(amplify-cli):Created an object for addSnapshotSerializer() --- .../src/__tests__/commands/uninstall.pkg.test.ts | 5 +++-- .../src/__tests__/commands/upgrade.pkg.test.ts | 4 ++-- packages/amplify-cli/src/snapshot-serializer.ts | 12 ------------ .../amplify-helpers/testUtils/snapshot-serializer.ts | 11 +++++++++++ packages/amplify-cli/tsconfig.json | 3 ++- 5 files changed, 18 insertions(+), 17 deletions(-) delete mode 100644 packages/amplify-cli/src/snapshot-serializer.ts create mode 100644 packages/amplify-cli/tests/amplify-helpers/testUtils/snapshot-serializer.ts diff --git a/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts b/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts index 395cc251171..98ebdcd950a 100644 --- a/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts +++ b/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts @@ -4,7 +4,7 @@ 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 { windowsPathSerializer } from '../../../tests/amplify-helpers/testUtils/snapshot-serializer'; import path from 'path'; jest.mock('execa'); @@ -45,7 +45,8 @@ const setRegPendingDelete_mock = setRegPendingDelete as jest.MockedFunction; @@ -46,7 +46,7 @@ const context_stub_typed = (context_stub as unknown) as $TSContext; // save original process.platform const originalPlatform = process.platform; -snapshotSerializer(); +expect.addSnapshotSerializer(windowsPathSerializer); describe('run upgrade using packaged CLI', () => { beforeEach(() => jest.clearAllMocks()); diff --git a/packages/amplify-cli/src/snapshot-serializer.ts b/packages/amplify-cli/src/snapshot-serializer.ts deleted file mode 100644 index ffdbcbf7484..00000000000 --- a/packages/amplify-cli/src/snapshot-serializer.ts +++ /dev/null @@ -1,12 +0,0 @@ -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, '\\')}"`; - }, - }); -} \ No newline at end of file diff --git a/packages/amplify-cli/tests/amplify-helpers/testUtils/snapshot-serializer.ts b/packages/amplify-cli/tests/amplify-helpers/testUtils/snapshot-serializer.ts new file mode 100644 index 00000000000..a1e40086138 --- /dev/null +++ b/packages/amplify-cli/tests/amplify-helpers/testUtils/snapshot-serializer.ts @@ -0,0 +1,11 @@ +const originalPlatform = process.platform; + +// converts inputs to toMatchInlineSnapshot() function into supported format +export const windowsPathSerializer = { + test(val) { + return typeof val === 'string' && originalPlatform === 'win32'; + }, + print(val) { + return `"${(val as string).replace(/\\/g, '\\')}"`; + } +}; diff --git a/packages/amplify-cli/tsconfig.json b/packages/amplify-cli/tsconfig.json index b00b1a2367c..183ca2745f7 100644 --- a/packages/amplify-cli/tsconfig.json +++ b/packages/amplify-cli/tsconfig.json @@ -7,7 +7,8 @@ "outDir": "lib" }, "include": [ - "src/**/*" + "src/**/*", + "tests/amplify-helpers/testUtils" ], "references": [ { "path": "../amplify-category-function" }, From 0b578d6516c41c106eaba312b6804aa8a7ea69d5 Mon Sep 17 00:00:00 2001 From: studpeps Date: Wed, 21 Jul 2021 00:43:15 +0530 Subject: [PATCH 08/10] Fixed spaces in path.join() --- .../amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts b/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts index 98ebdcd950a..1b2c3a48173 100644 --- a/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts +++ b/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts @@ -5,7 +5,7 @@ import * as fs from 'fs-extra'; import { hideSync } from 'hidefile'; import { setRegPendingDelete } from '../../utils/win-utils'; import { windowsPathSerializer } from '../../../tests/amplify-helpers/testUtils/snapshot-serializer'; -import path from 'path'; +import * as path from 'path'; jest.mock('execa'); const execa_mock = execa as jest.Mocked; @@ -111,7 +111,7 @@ describe('uninstall packaged CLI on windows', () => { expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "${path.join('homedir','.amplify','bin','amplify.exe')}", + "${path.join('homedir', '.amplify', 'bin', 'amplify.exe')}", "a/test/path/.amplify-pending-delete.exe", Object { "overwrite": true, From 7924291a6a8cde534a490384be6d6154d530375f Mon Sep 17 00:00:00 2001 From: studpeps Date: Wed, 21 Jul 2021 19:49:11 +0530 Subject: [PATCH 09/10] bug(amplify-cli):test function added to snapshot-serializer --- .../amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts | 2 +- .../amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts | 2 +- .../__tests__}/testUtils/snapshot-serializer.ts | 4 ++++ packages/amplify-cli/tsconfig.json | 3 +-- 4 files changed, 7 insertions(+), 4 deletions(-) rename packages/amplify-cli/{tests/amplify-helpers => src/__tests__}/testUtils/snapshot-serializer.ts (77%) diff --git a/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts b/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts index 1b2c3a48173..ca1dc98dab1 100644 --- a/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts +++ b/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts @@ -4,7 +4,7 @@ import execa from 'execa'; import * as fs from 'fs-extra'; import { hideSync } from 'hidefile'; import { setRegPendingDelete } from '../../utils/win-utils'; -import { windowsPathSerializer } from '../../../tests/amplify-helpers/testUtils/snapshot-serializer'; +import { windowsPathSerializer } from '../testUtils/snapshot-serializer'; import * as path from 'path'; jest.mock('execa'); diff --git a/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts b/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts index 28b702a71a6..5bf3f1b4b26 100644 --- a/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts +++ b/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts @@ -4,7 +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 { windowsPathSerializer } from '../../../tests/amplify-helpers/testUtils/snapshot-serializer'; +import { windowsPathSerializer } from '../testUtils/snapshot-serializer'; jest.mock('fs-extra'); const fs_mock = (fs as unknown) as jest.Mocked; diff --git a/packages/amplify-cli/tests/amplify-helpers/testUtils/snapshot-serializer.ts b/packages/amplify-cli/src/__tests__/testUtils/snapshot-serializer.ts similarity index 77% rename from packages/amplify-cli/tests/amplify-helpers/testUtils/snapshot-serializer.ts rename to packages/amplify-cli/src/__tests__/testUtils/snapshot-serializer.ts index a1e40086138..e79c3b66b90 100644 --- a/packages/amplify-cli/tests/amplify-helpers/testUtils/snapshot-serializer.ts +++ b/packages/amplify-cli/src/__tests__/testUtils/snapshot-serializer.ts @@ -9,3 +9,7 @@ export const windowsPathSerializer = { return `"${(val as string).replace(/\\/g, '\\')}"`; } }; + +test('validateWindowsPathSerializer', () => { + expect(windowsPathSerializer).toBeDefined(); +}); \ No newline at end of file diff --git a/packages/amplify-cli/tsconfig.json b/packages/amplify-cli/tsconfig.json index 183ca2745f7..30981d12a16 100644 --- a/packages/amplify-cli/tsconfig.json +++ b/packages/amplify-cli/tsconfig.json @@ -7,8 +7,7 @@ "outDir": "lib" }, "include": [ - "src/**/*", - "tests/amplify-helpers/testUtils" + "src/**/*" ], "references": [ { "path": "../amplify-category-function" }, From df61890dee62e2bdb5c1649889092a54e0c1af9a Mon Sep 17 00:00:00 2001 From: Stuti Prasad <43947328+studpeps@users.noreply.github.com> Date: Wed, 21 Jul 2021 20:10:26 +0530 Subject: [PATCH 10/10] Removed extra space in .json file --- packages/amplify-cli/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/amplify-cli/tsconfig.json b/packages/amplify-cli/tsconfig.json index 30981d12a16..b00b1a2367c 100644 --- a/packages/amplify-cli/tsconfig.json +++ b/packages/amplify-cli/tsconfig.json @@ -7,7 +7,7 @@ "outDir": "lib" }, "include": [ - "src/**/*" + "src/**/*" ], "references": [ { "path": "../amplify-category-function" },