UNPKG

2.96 kBJavaScriptView Raw
1'use strict';
2
3const { resolve } = require('path');
4
5const chai = require('chai');
6
7const { asyncForEach, asyncMap, execFile, exists, isDirectory, readDir, readFile } = require('../js/utils');
8
9describe('copyProjectSetup', function () {
10 const assert = chai.assert;
11 const copyProjectSetupPath = resolve(__dirname, '../js/copyProjectSetup.js');
12 const projectDir = resolve(__dirname, '../tests/fixtures/project-a');
13 const imagesDir = resolve(projectDir, 'images');
14 let configContent;
15
16 before(async () => {
17 configContent = await readFile(resolve(projectDir, 'setup/config.js'));
18 });
19
20 const copyProjectSetup = async (args = []) => {
21 const { stdout, stderr } = await execFile(copyProjectSetupPath, args, {
22 cwd: projectDir,
23 env: { ...process.env, COMPOSE_FILE: 'dc.prod.yml' }
24 });
25 if (stderr) {
26 console.error(' - stdout:', stdout);
27 throw new Error(stderr);
28 }
29 else if (stdout) {
30 // print whatever was logged in copyProjectSetup:
31 console.log(stdout);
32 }
33 return stdout;
34 };
35
36 const removeCopies = async () => {
37 await copyProjectSetup(['--clean']);
38 await asyncMap(readDir(imagesDir), async (file) => {
39 const setupDir = resolve(imagesDir, file, '__project_setup__');
40 assert.isFalse(await exists(setupDir));
41 });
42 };
43
44 const checkSetupContent = async (imageDir) => {
45 assert.isTrue(await isDirectory(imageDir));
46 const setupDir = resolve(imageDir, '__project_setup__');
47 assert.isTrue(await isDirectory(setupDir));
48 let file;
49
50 // check: config.js
51 file = resolve(setupDir, 'config.js');
52 assert.isTrue(await exists(file));
53 const copyContent = await readFile(file);
54 assert.deepEqual(copyContent, configContent);
55
56 // check: config.local.js
57 file = resolve(setupDir, 'config.local.js');
58 assert.isFalse(await exists(file));
59 };
60
61 it('remove copies', removeCopies);
62
63 it('copy setup', async () => {
64 await copyProjectSetup();
65 const dxServices = ['back-a', 'back-b', 'back-c', 'front-a', 'front-b'];
66 const otherServices = ['store-a'];
67 await asyncForEach(dxServices, async (serviceName) => {
68 await checkSetupContent(resolve(imagesDir, serviceName));
69 });
70 await asyncForEach(otherServices, async (serviceName) => {
71 const setupDir = resolve(imagesDir, serviceName, '__project_setup__');
72 assert.isFalse(await exists(setupDir));
73 });
74 });
75
76 it('remove copies', removeCopies);
77
78 it('copy setup for some services', async () => {
79 const services = ['back-a', 'front-a'];
80 await copyProjectSetup(services);
81 await asyncMap(readDir(imagesDir), async (fileName) => {
82 if (services.includes(fileName)) {
83 await checkSetupContent(resolve(imagesDir, fileName));
84 }
85 else {
86 const setupDir = resolve(imagesDir, fileName, '__project_setup__');
87 assert.isFalse(await exists(setupDir));
88 }
89 });
90 });
91
92 it('remove copies', removeCopies);
93
94});