diff --git a/src/utils/git-ignore.ts b/src/utils/git-ignore.ts index e1a48643..1f160a31 100644 --- a/src/utils/git-ignore.ts +++ b/src/utils/git-ignore.ts @@ -24,5 +24,6 @@ export function modifyGitIgnore(projectDir: string) { } else { cli.warn('No .gitignore file found!'); cli.log('If your project is using a different version control system, please make sure the following paths are not tracked:'); + pathsToIgnore.forEach(path => cli.log(' ' + path)); } } diff --git a/src/utils/usage-analytics.ts b/src/utils/usage-analytics.ts index 637e8276..5acc95b5 100644 --- a/src/utils/usage-analytics.ts +++ b/src/utils/usage-analytics.ts @@ -6,7 +6,7 @@ import cli from 'cli-ux'; import * as fs from 'fs'; import * as path from 'path'; -export async function usageAnalytics(projectDir: string, agreeToAnalytics: boolean, salt?: string) { +export async function usageAnalytics(projectDir: string, agreeToAnalytics: boolean | undefined, salt?: string) { const analyticsFilePath = path.resolve(projectDir, 'sap-cloud-sdk-analytics.json'); if (agreeToAnalytics === false) { return fs.writeFileSync(analyticsFilePath, JSON.stringify({ enabled: false })); diff --git a/test/init.spec.ts b/test/init.spec.ts index 1dff2d7b..e22e5a08 100644 --- a/test/init.spec.ts +++ b/test/init.spec.ts @@ -38,8 +38,8 @@ function getCleanProjectDir(name: string) { } describe('Init', () => { - const expressAppDir = 'test/express/'; - const nestAppDir = 'test/nest/'; + const expressAppDir = path.resolve('test', 'express'); + const nestAppDir = path.resolve('test', 'nest'); beforeAll(() => { if (!fs.existsSync(pathPrefix)) { diff --git a/test/utils/__snapshots__/jest-config.spec.ts.snap b/test/utils/__snapshots__/jest-config.spec.ts.snap new file mode 100644 index 00000000..212d2491 --- /dev/null +++ b/test/utils/__snapshots__/jest-config.spec.ts.snap @@ -0,0 +1,43 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Jest Config Utils returns jest config object for tests 1`] = ` +Object { + "collectCoverage": true, + "coverageDirectory": "../s4hana_pipeline/reports/coverage-reports/backend-unit", + "coverageReporters": Array [ + "text", + "cobertura", + ], + "reporters": Array [ + "default", + Array [ + "jest-junit", + Object { + "outputDirectory": "./s4hana_pipeline/reports/backend-unit", + "suiteName": "backend unit tests", + }, + ], + ], +} +`; + +exports[`Jest Config Utils returns jest config object for tests 2`] = ` +Object { + "collectCoverage": true, + "coverageDirectory": "../s4hana_pipeline/reports/coverage-reports/backend-integration", + "coverageReporters": Array [ + "text", + "cobertura", + ], + "reporters": Array [ + "default", + Array [ + "jest-junit", + Object { + "outputDirectory": "./s4hana_pipeline/reports/backend-integration", + "suiteName": "backend unit tests", + }, + ], + ], +} +`; diff --git a/test/utils/__snapshots__/package-json.spec.ts.snap b/test/utils/__snapshots__/package-json.spec.ts.snap new file mode 100644 index 00000000..80a4ef8b --- /dev/null +++ b/test/utils/__snapshots__/package-json.spec.ts.snap @@ -0,0 +1,172 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Package Json Utils add scripts, dependencies and test config for existing project 1`] = ` +Object { + "author": "", + "dependencies": Object { + "@nestjs/common": "^6.0.0", + "@nestjs/core": "^6.0.0", + "@nestjs/platform-express": "^6.0.0", + "@sap/cloud-sdk-core": "^undefined", + "reflect-metadata": "^0.1.12", + "rimraf": "^2.6.2", + "rxjs": "^6.3.3", + }, + "description": "", + "devDependencies": Object { + "@nestjs/testing": "6.1.1", + "@sap-cloud-sdk/cli": "^undefined", + "@sap/cloud-sdk-test-util": "^undefined", + "@types/express": "4.16.1", + "@types/jest": "24.0.11", + "@types/node": "11.13.4", + "@types/supertest": "2.0.7", + "jest": "24.7.1", + "prettier": "1.17.0", + "supertest": "4.0.2", + "ts-jest": "24.0.2", + "ts-node": "8.1.0", + "tsc-watch": "2.2.1", + "tsconfig-paths": "3.8.0", + "tslint": "5.16.0", + "typescript": "3.4.3", + }, + "jest": Object { + "coverageDirectory": "../coverage", + "moduleFileExtensions": Array [ + "js", + "json", + "ts", + ], + "rootDir": "src", + "testEnvironment": "node", + "testRegex": ".spec.ts$", + "transform": Object { + "^.+\\\\.(t|j)s$": "ts-jest", + }, + }, + "license": "MIT", + "name": "nest", + "scripts": Object { + "build": "rimraf dist && tsc -p tsconfig.build.json", + "ci-backend-unit-test": "echo \\"Test your application and write results in a JUnit format to \`s4hana_pipeline/reports/backend-unit/\` and coverage in a cobertura format to \`s4hana_pipeline/reports/coverage/backend-unit/\`\\"", + "ci-build": "echo \\"Use this to compile or minify your application\\"", + "ci-integration-test": "echo \\"Test your application and write results in a JUnit format to \`s4hana_pipeline/reports/backend-integration/\` and coverage in a cobertura format to \`s4hana_pipeline/reports/coverage/backend-integration/\`\\"", + "ci-package": "sap-cloud-sdk package --include=\\"package.json,package-lock.json,index.js,dist/**/*\\"", + "format": "prettier --write \\"src/**/*.ts\\"", + "lint": "tslint -p tsconfig.json -c tslint.json", + "start": "ts-node -r tsconfig-paths/register src/main.ts", + "start:debug": "tsc-watch -p tsconfig.build.json --onSuccess \\"node --inspect-brk dist/main.js\\"", + "start:dev": "tsc-watch -p tsconfig.build.json --onSuccess \\"node dist/main.js\\"", + "start:prod": "node dist/main.js", + "test": "jest", + "test:cov": "jest --coverage", + "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", + "test:e2e": "jest --config ./test/jest-e2e.json", + "test:watch": "jest --watch", + }, + "version": "0.0.1", +} +`; + +exports[`Package Json Utils add scripts, dependencies and test config for scaffolded project 1`] = ` +Object { + "author": "", + "dependencies": Object { + "@nestjs/common": "^6.0.0", + "@nestjs/core": "^6.0.0", + "@nestjs/platform-express": "^6.0.0", + "@sap/cloud-sdk-core": "^undefined", + "reflect-metadata": "^0.1.12", + "rimraf": "^2.6.2", + "rxjs": "^6.3.3", + }, + "description": "", + "devDependencies": Object { + "@nestjs/testing": "6.1.1", + "@sap-cloud-sdk/cli": "^undefined", + "@sap/cloud-sdk-test-util": "^undefined", + "@types/express": "4.16.1", + "@types/jest": "24.0.11", + "@types/node": "11.13.4", + "@types/supertest": "2.0.7", + "jest": "24.7.1", + "jest-junit": "^undefined", + "prettier": "1.17.0", + "supertest": "4.0.2", + "ts-jest": "24.0.2", + "ts-node": "8.1.0", + "tsc-watch": "2.2.1", + "tsconfig-paths": "3.8.0", + "tslint": "5.16.0", + "typescript": "3.4.3", + }, + "jest": Object { + "collectCoverage": true, + "coverageDirectory": "../s4hana_pipeline/reports/coverage-reports/backend-unit", + "coverageReporters": Array [ + "text", + "cobertura", + ], + "moduleFileExtensions": Array [ + "js", + "json", + "ts", + ], + "reporters": Array [ + "default", + Array [ + "jest-junit", + Object { + "outputDirectory": "./s4hana_pipeline/reports/backend-unit", + "suiteName": "backend unit tests", + }, + ], + ], + "rootDir": "src", + "testEnvironment": "node", + "testRegex": ".spec.ts$", + "transform": Object { + "^.+\\\\.(t|j)s$": "ts-jest", + }, + }, + "license": "MIT", + "name": "nest", + "scripts": Object { + "build": "rimraf dist && tsc -p tsconfig.build.json", + "ci-backend-unit-test": "jest --ci", + "ci-build": "npm run build", + "ci-e2e": "echo \\"Test your application and write results in a JUnit format to \`s4hana_pipeline/reports/e2e/\`\\"", + "ci-frontend-unit-test": "echo \\"Test your application and write results in a JUnit format to \`s4hana_pipeline/reports/frontend-unit/\` and coverage in a cobertura format to \`s4hana_pipeline/reports/coverage/frontend-unit/\`\\"", + "ci-integration-test": "jest --ci --config ./test/jest-e2e.json", + "ci-package": "sap-cloud-sdk package", + "deploy": "npm run ci-build && npm run ci-package && cf push", + "format": "prettier --write \\"src/**/*.ts\\"", + "lint": "tslint -p tsconfig.json -c tslint.json", + "start": "ts-node -r tsconfig-paths/register src/main.ts", + "start:debug": "tsc-watch -p tsconfig.build.json --onSuccess \\"node --inspect-brk dist/main.js\\"", + "start:dev": "tsc-watch -p tsconfig.build.json --onSuccess \\"node dist/main.js\\"", + "start:prod": "node dist/main.js", + "test": "jest", + "test:cov": "jest --coverage", + "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", + "test:e2e": "jest --config ./test/jest-e2e.json", + "test:watch": "jest --watch", + }, + "version": "0.0.1", +} +`; + +exports[`Package Json Utils should parse the package.json 1`] = ` +Array [ + "author", + "dependencies", + "description", + "devDependencies", + "jest", + "license", + "name", + "scripts", + "version", +] +`; diff --git a/test/utils/__snapshots__/scaffold.spec.ts.snap b/test/utils/__snapshots__/scaffold.spec.ts.snap new file mode 100644 index 00000000..b4db303f --- /dev/null +++ b/test/utils/__snapshots__/scaffold.spec.ts.snap @@ -0,0 +1,17 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Scaffold Utils should build the scaffold 1`] = ` +Array [ + ".git", + ".gitignore", + ".prettierrc", + "README.md", + "nest-cli.json", + "package.json", + "src", + "test", + "tsconfig.build.json", + "tsconfig.json", + "tslint.json", +] +`; diff --git a/test/utils/__snapshots__/templates.spec.ts.snap b/test/utils/__snapshots__/templates.spec.ts.snap new file mode 100644 index 00000000..20fab4dc --- /dev/null +++ b/test/utils/__snapshots__/templates.spec.ts.snap @@ -0,0 +1,40 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Templates Utils should copy files locally 1`] = ` +Array [ + ".npmrc", + "Jenkinsfile", + "credentials.json", + "deployment", + "manifest.yml", + "pipeline_config.yml", + "s4hana_pipeline", + "systems.json", +] +`; + +exports[`Templates Utils should return information which files to copy where 1`] = ` +Array [ + ".gitkeep", + ".gitkeep", + ".gitkeep", + ".gitkeep", + ".gitkeep", + ".npmrc", + "Jenkinsfile", + "credentials.json", + "manifest.yml", + "pipeline_config.yml", + "systems.json", +] +`; + +exports[`Templates Utils should return information which files to copy where 2`] = ` +Array [ + ".npmrc", + "manifest.yml", + "package.json", + "xs-app.json", + "xs-security.json", +] +`; diff --git a/test/utils/git-ignore.spec.ts b/test/utils/git-ignore.spec.ts new file mode 100644 index 00000000..9e0c84d5 --- /dev/null +++ b/test/utils/git-ignore.spec.ts @@ -0,0 +1,77 @@ +/*! + * Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. + */ +const log = jest.fn(); +const warn = jest.fn(message => console.log('MOCKED WARNING: ', message)); + +jest.mock('cli-ux', () => { + // Mocking needs to happen before the command is imported + const cli = jest.requireActual('cli-ux'); + return { + ...cli, + default: { + ...cli.default, + log, + warn + } + }; +}); + +import * as fs from 'fs'; +import * as path from 'path'; +import * as rm from 'rimraf'; +import { modifyGitIgnore } from '../../src/utils'; + +const pathPrefix = path.resolve(__dirname, __filename.replace(/\./g, '-')).replace('-ts', ''); + +function getCleanProjectDir(name: string) { + const projectDir = path.resolve(pathPrefix, name); + if (fs.existsSync(projectDir)) { + rm.sync(projectDir); + } + fs.mkdirSync(projectDir, { recursive: true }); + return projectDir; +} + +describe('Git Ignore Utils', () => { + afterAll(() => { + rm.sync(pathPrefix); + }); + + it('should add paths to empty git ignore', () => { + const projectDir = getCleanProjectDir('empty-git-ignore'); + fs.writeFileSync(`${projectDir}/.gitignore`, ''); + + modifyGitIgnore(projectDir); + + const gitIgnoreContent = fs.readFileSync(`${projectDir}/.gitignore`, { encoding: 'utf8' }).split('\n'); + expect(gitIgnoreContent).toContain('/s4hana_pipeline'); + expect(gitIgnoreContent).toContain('credentials.json'); + }); + + it('should add paths to existing git ignore', () => { + const projectDir = getCleanProjectDir('existing-git-ignore'); + fs.writeFileSync( + `${projectDir}/.gitignore`, + `myPath + foobar + + !@#$%^&^ + \\n` + ); + modifyGitIgnore(projectDir); + + const gitIgnoreContent = fs.readFileSync(`${projectDir}/.gitignore`, { encoding: 'utf8' }).split('\n'); + expect(gitIgnoreContent).toContain('/s4hana_pipeline'); + expect(gitIgnoreContent).toContain('credentials.json'); + }); + + it('warn if there is no git ignore', () => { + const projectDir = getCleanProjectDir('no-git-ignore'); + + modifyGitIgnore(projectDir); + + expect(warn).toHaveBeenCalledWith('No .gitignore file found!'); + expect(log).toHaveBeenCalledTimes(4); + }); +}); diff --git a/test/utils/jest-config.spec.ts b/test/utils/jest-config.spec.ts new file mode 100644 index 00000000..891904b3 --- /dev/null +++ b/test/utils/jest-config.spec.ts @@ -0,0 +1,39 @@ +/*! + * Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. + */ +import * as fs from 'fs'; +import * as path from 'path'; +import * as rm from 'rimraf'; +import { getJestConfig, modifyJestConfig } from '../../src/utils'; + +const pathPrefix = path.resolve(__dirname, __filename.replace(/\./g, '-')).replace('-ts', ''); + +function getCleanProjectDir(name: string) { + const projectDir = path.resolve(pathPrefix, name); + if (fs.existsSync(projectDir)) { + rm.sync(projectDir); + } + fs.mkdirSync(projectDir, { recursive: true }); + return projectDir; +} + +describe('Jest Config Utils', () => { + afterAll(() => { + rm.sync(pathPrefix); + }); + + it('returns jest config object for tests', () => { + expect(getJestConfig(true)).toMatchSnapshot(); + expect(getJestConfig(false)).toMatchSnapshot(); + }); + + it('returns adds jest config to file', () => { + const projectDir = getCleanProjectDir('jest-config'); + const jestConfigPath = path.resolve(projectDir, 'jest-e2e.json'); + fs.copyFileSync(path.resolve('test', 'nest', 'test', 'jest-e2e.json'), jestConfigPath); + + modifyJestConfig(jestConfigPath, getJestConfig(false)); + + expect(JSON.parse(fs.readFileSync(jestConfigPath, { encoding: 'utf8' }))).toMatchObject(getJestConfig(false)); + }); +}); diff --git a/test/utils/package-json.spec.ts b/test/utils/package-json.spec.ts new file mode 100644 index 00000000..4fcb5ba8 --- /dev/null +++ b/test/utils/package-json.spec.ts @@ -0,0 +1,58 @@ +/*! + * Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. + */ +jest.mock('execa', () => jest.fn().mockResolvedValue('1.0.0')); + +import * as execa from 'execa'; +import * as fs from 'fs'; +import * as path from 'path'; +import * as rm from 'rimraf'; +import { installDependencies, modifyPackageJson, parsePackageJson } from '../../src/utils'; + +const pathPrefix = path.resolve(__dirname, __filename.replace(/\./g, '-')).replace('-ts', ''); + +function getCleanProjectDir(name: string) { + const projectDir = path.resolve(pathPrefix, name); + if (fs.existsSync(projectDir)) { + rm.sync(projectDir); + } + fs.mkdirSync(projectDir, { recursive: true }); + return projectDir; +} + +describe('Package Json Utils', () => { + afterAll(() => { + rm.sync(pathPrefix); + }); + + it('should call `npm install`', () => { + installDependencies('', true); + expect(execa).toHaveBeenCalledWith('npm', ['install'], { cwd: '', stdio: 'inherit' }); + }); + + it('should parse the package.json', () => { + const projectDir = getCleanProjectDir('parse-package-json'); + const packageJsonPath = path.resolve(projectDir, 'package.json'); + fs.copyFileSync(path.resolve('test', 'nest', 'package.json'), packageJsonPath); + + expect(Object.keys(parsePackageJson(projectDir)).sort()).toMatchSnapshot(); + }); + + it('add scripts, dependencies and test config for existing project', async () => { + const projectDir = getCleanProjectDir('modify-package-json-existing'); + const packageJsonPath = path.resolve(projectDir, 'package.json'); + fs.copyFileSync(path.resolve('test', 'nest', 'package.json'), packageJsonPath); + + await modifyPackageJson(projectDir, false, false); + expect(parsePackageJson(projectDir)).toMatchSnapshot(); + }); + + it('add scripts, dependencies and test config for scaffolded project', async () => { + const projectDir = getCleanProjectDir('modify-package-json-existing'); + const packageJsonPath = path.resolve(projectDir, 'package.json'); + fs.copyFileSync(path.resolve('test', 'nest', 'package.json'), packageJsonPath); + + await modifyPackageJson(projectDir, true, true); + expect(parsePackageJson(projectDir)).toMatchSnapshot(); + }); +}); diff --git a/test/utils/scaffold.spec.ts b/test/utils/scaffold.spec.ts new file mode 100644 index 00000000..8afeb823 --- /dev/null +++ b/test/utils/scaffold.spec.ts @@ -0,0 +1,56 @@ +/*! + * Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. + */ +const confirm = jest.fn().mockResolvedValue(true); +jest.mock('cli-ux', () => { + // Mocking needs to happen before the command is imported + const cli = jest.requireActual('cli-ux'); + return { + ...cli, + default: { + ...cli.default, + confirm + } + }; +}); + +import * as fs from 'fs'; +import * as path from 'path'; +import * as rm from 'rimraf'; +import { buildScaffold, shouldBuildScaffold } from '../../src/utils'; + +const pathPrefix = path.resolve(__dirname, __filename.replace(/\./g, '-')).replace('-ts', ''); + +function getCleanProjectDir(name: string) { + const projectDir = path.resolve(pathPrefix, name); + if (fs.existsSync(projectDir)) { + rm.sync(projectDir); + } + fs.mkdirSync(projectDir, { recursive: true }); + return projectDir; +} + +describe('Scaffold Utils', () => { + afterAll(() => { + rm.sync(pathPrefix); + }); + + it('should determine if scaffold is needed', async () => { + const projectDir = getCleanProjectDir('should-build-scaffold'); + + expect(await shouldBuildScaffold(projectDir, false)).toBe(true); + expect(await shouldBuildScaffold(projectDir, true)).toBe(true); + + const packageJsonPath = path.resolve(projectDir, 'package.json'); + fs.copyFileSync(path.resolve('test', 'nest', 'package.json'), packageJsonPath); + expect(await shouldBuildScaffold(projectDir, false)).toBe(false); + }); + + it('should build the scaffold', async () => { + const projectDir = getCleanProjectDir('build-scaffold'); + + await buildScaffold(projectDir, false); + + expect(fs.readdirSync(projectDir).sort()).toMatchSnapshot(); + }, 60000); +}); diff --git a/test/utils/templates.spec.ts b/test/utils/templates.spec.ts new file mode 100644 index 00000000..228a5496 --- /dev/null +++ b/test/utils/templates.spec.ts @@ -0,0 +1,79 @@ +/*! + * Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. + */ +import * as fs from 'fs'; +import * as path from 'path'; +import * as rm from 'rimraf'; +import { copyFiles, ensureDirectoryExistence, findConflicts, readTemplates } from '../../src/utils'; + +const pathPrefix = path.resolve(__dirname, __filename.replace(/\./g, '-')).replace('-ts', ''); + +function getCleanProjectDir(name: string) { + const projectDir = path.resolve(pathPrefix, name); + if (fs.existsSync(projectDir)) { + rm.sync(projectDir); + } + fs.mkdirSync(projectDir, { recursive: true }); + return projectDir; +} + +describe('Templates Utils', () => { + afterAll(() => { + rm.sync(pathPrefix); + }); + + it('ensure directory existence', () => { + const projectDir = getCleanProjectDir('ensure-dir-existence'); + const dir = path.resolve(projectDir, 'foo', 'bar'); + const file = path.resolve(dir, 'abc', 'package.json'); + + ensureDirectoryExistence(dir, true); + expect(fs.existsSync(dir)).toBe(true); + + ensureDirectoryExistence(file); + expect(fs.existsSync(path.resolve(dir, 'abc'))).toBe(true); + }); + + it('should return information which files to copy where', () => { + const initCopyInfo = readTemplates({ + from: [path.resolve(__dirname, '..', '..', 'src', 'templates', 'init')], + to: 'abcdef' + }); + expect(initCopyInfo.map(copyInfo => path.basename(copyInfo.fileName)).sort()).toMatchSnapshot(); + + const appRouterCopyInfo = readTemplates({ + from: [path.resolve(__dirname, '..', '..', 'src', 'templates', 'add-approuter')], + to: 'blablabla' + }); + expect(appRouterCopyInfo.map(copyInfo => path.basename(copyInfo.fileName)).sort()).toMatchSnapshot(); + }); + + it('should find conflicts', async () => { + const projectDir = getCleanProjectDir('find-conflicts'); + fs.writeFileSync(path.resolve(projectDir, '.npmrc'), 'foobar'); + findConflicts( + readTemplates({ + from: [path.resolve(__dirname, '..', '..', 'src', 'templates', 'init')], + to: projectDir + }), + true + ); + expect(fs.existsSync(path.resolve(projectDir, '.npmrc'))).toBe(false); + }); + + it('should copy files locally', async () => { + const projectDir = getCleanProjectDir('copy-files-locally'); + copyFiles( + readTemplates({ + from: [path.resolve(__dirname, '..', '..', 'src', 'templates', 'init')], + to: projectDir + }), + {} + ); + expect(fs.readdirSync(projectDir).sort()).toMatchSnapshot(); + }); + + // it('should copy files remotely', async () => { + // const projectDir = getCleanProjectDir('copy-files-remotely'); + // }); +}); diff --git a/test/utils/usage-analytics.spec.ts b/test/utils/usage-analytics.spec.ts new file mode 100644 index 00000000..c75ffd1d --- /dev/null +++ b/test/utils/usage-analytics.spec.ts @@ -0,0 +1,59 @@ +/*! + * Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. + */ +const confirm = jest.fn().mockResolvedValue(false); + +jest.mock('cli-ux', () => { + // Mocking needs to happen before the command is imported + const cli = jest.requireActual('cli-ux'); + return { + ...cli, + default: { + ...cli.default, + confirm + } + }; +}); + +import * as fs from 'fs'; +import * as path from 'path'; +import * as rm from 'rimraf'; +import { usageAnalytics } from '../../src/utils'; + +const pathPrefix = path.resolve(__dirname, __filename.replace(/\./g, '-')).replace('-ts', ''); + +function getCleanProjectDir(name: string) { + const projectDir = path.resolve(pathPrefix, name); + if (fs.existsSync(projectDir)) { + rm.sync(projectDir); + } + fs.mkdirSync(projectDir, { recursive: true }); + return projectDir; +} + +function readConsentFile(projectDir: string) { + const filePath = path.resolve(projectDir, 'sap-cloud-sdk-analytics.json'); + return JSON.parse(fs.readFileSync(filePath, { encoding: 'utf8' })); +} + +describe('Usage Analytics Utils', () => { + afterAll(() => { + rm.sync(pathPrefix); + }); + + it('Create usage analytics consent file', async () => { + const projectDir = getCleanProjectDir('usage-analytics'); + + await usageAnalytics(projectDir, true); + expect(readConsentFile(projectDir).enabled).toBe(true); + + await usageAnalytics(projectDir, true, 'TEST'); + expect(readConsentFile(projectDir)).toEqual({ enabled: true, salt: 'TEST' }); + + await usageAnalytics(projectDir, undefined); + expect(readConsentFile(projectDir).enabled).toBe(false); + + await usageAnalytics(projectDir, false); + expect(readConsentFile(projectDir).enabled).toBe(false); + }); +});