Skip to content

Commit

Permalink
feat: add light preview (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lakhdar authored Jun 15, 2021
1 parent 75fdb92 commit 3269a59
Show file tree
Hide file tree
Showing 8 changed files with 658 additions and 35 deletions.
6 changes: 3 additions & 3 deletions packages/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"dependencies": {
"@angular/cli": "^11.1.4",
"@coveord/platform-client": "^19.14.0",
"@coveord/platform-client": "^20.2.0",
"@oclif/command": "^1",
"@oclif/config": "^1",
"@oclif/plugin-help": "^3",
Expand All @@ -16,6 +16,7 @@
"@vue/cli": "^4.5.11",
"abortcontroller-polyfill": "^1.7.1",
"archiver": "^5.3.0",
"chalk": "^4.1.1",
"cli-ux": "^5.5.1",
"coveo.analytics": "^2.18.4",
"create-react-app": "^4.0.3",
Expand All @@ -34,6 +35,7 @@
"@coveo/cra-template": "^1.3.0",
"@coveo/vue-cli-plugin-typescript": "^1.3.0",
"@oclif/dev-cli": "^1.26.0",
"@oclif/errors": "^1.3.4",
"@oclif/test": "^1",
"@types/archiver": "^5.1.0",
"@types/cli-progress": "^3.9.1",
Expand Down
220 changes: 220 additions & 0 deletions packages/cli/src/commands/org/config/preview.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
jest.mock('../../../lib/config/config');
jest.mock('../../../hooks/analytics/analytics');
jest.mock('../../../hooks/prerun/prerun');
jest.mock('../../../lib/platform/authenticatedClient');
jest.mock('../../../lib/snapshot/snapshot');
jest.mock('../../../lib/snapshot/snapshotFactory');
jest.mock('../../../lib/project/project');
jest.mock('@oclif/errors');

import {mocked} from 'ts-jest/utils';
import {test} from '@oclif/test';
import {Project} from '../../../lib/project/project';
import {join, normalize} from 'path';
import {cwd} from 'process';
import {Config} from '../../../lib/config/config';
import {SnapshotFactory} from '../../../lib/snapshot/snapshotFactory';
import {Snapshot} from '../../../lib/snapshot/snapshot';
import {warn, error} from '@oclif/errors';

const mockedSnapshotFactory = mocked(SnapshotFactory, true);
const mockedConfig = mocked(Config);
const mockedProject = mocked(Project);
const mockedWarn = mocked(warn);
const mockedError = mocked(error);
const mockedConfigGet = jest.fn();
const mockedDeleteTemporaryZipFile = jest.fn();
const mockedDeleteSnapshot = jest.fn();
const mockedSaveDetailedReport = jest.fn();
const mockedRequiresSynchronization = jest.fn();
const mockedPreviewSnapshot = jest.fn();
const mockedLastReport = jest.fn();

const mockProject = () => {
mockedProject.mockImplementation(
() =>
({
compressResources: () =>
Promise.resolve(normalize(join('path', 'to', 'resources.zip'))),
deleteTemporaryZipFile: mockedDeleteTemporaryZipFile,
} as unknown as Project)
);
};

const mockConfig = () => {
mockedConfigGet.mockReturnValue(
Promise.resolve({
region: 'us-east-1',
organization: 'foo',
environment: 'prod',
})
);

// TODO: use prototype
mockedConfig.mockImplementation(
() =>
({
get: mockedConfigGet,
} as unknown as Config)
);
};

const mockSnapshotFactory = async (validResponse: unknown) => {
mockedSnapshotFactory.createFromZip.mockReturnValue(
Promise.resolve({
validate: () => Promise.resolve(validResponse),
preview: mockedPreviewSnapshot,
delete: mockedDeleteSnapshot,
saveDetailedReport: mockedSaveDetailedReport,
requiresSynchronization: mockedRequiresSynchronization,
latestReport: mockedLastReport,
id: 'banana-snapshot',
targetId: 'potato-org',
} as unknown as Snapshot)
);
};

const mockSnapshotFactoryReturningValidSnapshot = async () => {
await mockSnapshotFactory({isValid: true, report: {}});
};

const mockSnapshotFactoryReturningInvalidSnapshot = async () => {
await mockSnapshotFactory({isValid: false, report: {}});
};

describe('org:config:preview', () => {
beforeAll(() => {
mockConfig();
mockProject();
});

describe('when the report contains no resources in error', () => {
beforeAll(async () => {
await mockSnapshotFactoryReturningValidSnapshot();
});

afterAll(() => {
mockedSnapshotFactory.mockReset();
});

test.command(['org:config:preview']).it('should use cwd as project', () => {
expect(mockedProject).toHaveBeenCalledWith(cwd());
});

test
.command(['org:config:preview', '-p', 'path/to/project'])
.it('should use specifeid path for project', () => {
expect(mockedProject).toHaveBeenCalledWith(
normalize(join('path', 'to', 'project'))
);
});

test
.command(['org:config:preview'])
.it('should work with default connected org', () => {
expect(mockedSnapshotFactory.createFromZip).toHaveBeenCalledWith(
normalize(join('path', 'to', 'resources.zip')),
'foo'
);
});

test
.command(['org:config:preview', '-t', 'myorg'])
.it('should work with specified target org', () => {
expect(mockedSnapshotFactory.createFromZip).toHaveBeenCalledWith(
normalize(join('path', 'to', 'resources.zip')),
'myorg'
);
});

test
.command(['org:config:preview'])
.it('should preview the snapshot', () => {
expect(mockedPreviewSnapshot).toHaveBeenCalledTimes(1);
});

test
.command(['org:config:preview'])
.it('should delete the compressed folder', () => {
expect(mockedDeleteTemporaryZipFile).toHaveBeenCalledTimes(1);
});

test
.command(['org:config:preview'])
.it('should delete the snapshot', () => {
expect(mockedDeleteSnapshot).toHaveBeenCalledTimes(1);
});
});

describe('when the report contains resources in error', () => {
beforeAll(async () => {
await mockSnapshotFactoryReturningInvalidSnapshot();
});

beforeEach(() => {
mockedRequiresSynchronization.mockReturnValueOnce(false);
mockedSaveDetailedReport.mockReturnValueOnce(
normalize(join('saved', 'snapshot'))
);
});

afterAll(() => {
mockedSnapshotFactory.mockReset();
});

test
.command(['org:config:preview'])
.it('should throw an error for invalid snapshots', () => {
expect(mockedError).toHaveBeenCalledWith(
expect.stringContaining('Invalid snapshot'),
{}
);
});

test
.command(['org:config:preview'])
.it('should print an URL to the snapshot page', () => {
expect(mockedError).toHaveBeenCalledWith(
expect.stringContaining(
'https://platform.cloud.coveo.com/admin/#potato-org/organization/resource-snapshots/banana-snapshot'
),
{}
);
});
});

describe('when the snapshot is not in sync with the target org', () => {
beforeAll(async () => {
await mockSnapshotFactoryReturningInvalidSnapshot();
});

beforeEach(() => {
mockedRequiresSynchronization.mockReturnValueOnce(true);
mockedSaveDetailedReport.mockReturnValueOnce(join('saved', 'snapshot'));
});

afterAll(() => {
mockedSnapshotFactory.mockReset();
});

test
.command(['org:config:preview'])
.it('should have detected some conflicts', () => {
expect(mockedWarn).toHaveBeenCalledWith(
expect.stringContaining(
'Some conflicts were detected while comparing changes between the snapshot and the target organization'
)
);
});

test
.command(['org:config:preview'])
.it('should print an url to the synchronization page', () => {
expect(mockedWarn).toHaveBeenCalledWith(
expect.stringContaining(
'https://platform.cloud.coveo.com/admin/#potato-org/organization/resource-snapshots/banana-snapshot/synchronization'
)
);
});
});
});
27 changes: 16 additions & 11 deletions packages/cli/src/commands/org/config/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {Project} from '../../../lib/project/project';
import {SnapshotFactory} from '../../../lib/snapshot/snapshotFactory';
import {platformUrl} from '../../../lib/platform/environment';
import {Snapshot} from '../../../lib/snapshot/snapshot';
import {red, green} from 'chalk';
import {normalize} from 'path';

export interface CustomFile extends ReadStream {
type?: string;
Expand Down Expand Up @@ -42,7 +44,7 @@ export default class Preview extends Command {
@Preconditions(IsAuthenticated())
public async run() {
const {flags} = this.parse(Preview);
const project = new Project(flags.projectPath);
const project = new Project(normalize(flags.projectPath));
const pathToZip = await project.compressResources();
const targetOrg = await this.getTargetOrg();

Expand All @@ -55,15 +57,18 @@ export default class Preview extends Command {
const {isValid} = await snapshot.validate();

if (!isValid) {
this.handleInvalidSnapshot(snapshot);
} else {
await snapshot.preview();
await this.handleInvalidSnapshot(snapshot);
}

cli.action.stop(isValid ? green('✔') : red.bold('!'));

await snapshot.preview();

if (isValid) {
await snapshot.delete();
}

project.deleteTemporaryZipFile();

cli.action.stop();
}

public async getTargetOrg() {
Expand All @@ -82,20 +87,20 @@ export default class Preview extends Command {
const report = snapshot.latestReport;

if (snapshot.requiresSynchronization()) {
cli.action.start('Synchronization');

const synchronizationPlanUrl = await this.getSynchronizationPage(
snapshot
);
this.warn(
dedent`Some conflicts were detected while comparing changes between the snapshot and the target organization.
dedent`
Some conflicts were detected while comparing changes between the snapshot and the target organization.
Click on the URL below to synchronize your snapshot with your organization before running the command again.
${synchronizationPlanUrl}`
${synchronizationPlanUrl}
`
);
return;
}

const snapshotUrl = this.getSnapshotPage(snapshot);
const snapshotUrl = await this.getSnapshotPage(snapshot);

this.error(
dedent`Invalid snapshot - ${report.resultCode}.
Expand Down
Loading

0 comments on commit 3269a59

Please sign in to comment.