2021-12-31 13:28:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
2026-10-11 13:35:21 +02:00
|
|
|
const fs = require('fs');
|
2021-12-31 13:28:04 +01:00
|
|
|
const util = require('util');
|
|
|
|
|
const exec = util.promisify(require('child_process').exec);
|
|
|
|
|
|
|
|
|
|
jest.setTimeout(20000);
|
|
|
|
|
|
|
|
|
|
describe('Test the config-sync CLI', () => {
|
2022-09-04 10:42:52 +02:00
|
|
|
afterAll(async () => {
|
|
|
|
|
// Remove the generated files and the DB.
|
|
|
|
|
await exec('rm -rf config/sync');
|
|
|
|
|
await exec('rm -rf .tmp');
|
|
|
|
|
});
|
|
|
|
|
|
2021-12-31 13:28:04 +01:00
|
|
|
test('Export', async () => {
|
2023-10-22 12:36:28 +02:00
|
|
|
const { stdout: exportOutput } = await exec('yarn cs export -y');
|
|
|
|
|
expect(exportOutput).toContain('Finished export');
|
|
|
|
|
const { stdout: diffOutput } = await exec('yarn cs diff');
|
|
|
|
|
expect(diffOutput).toContain('No differences between DB and sync directory');
|
2021-12-31 13:28:04 +01:00
|
|
|
});
|
2023-10-22 12:36:28 +02:00
|
|
|
|
|
|
|
|
test('Import (delete)', async () => {
|
|
|
|
|
// Remove a file to trigger a delete.
|
|
|
|
|
await exec('mv config/sync/admin-role.strapi-editor.json .tmp');
|
|
|
|
|
const { stdout: importOutput } = await exec('yarn cs import -y');
|
|
|
|
|
expect(importOutput).toContain('Finished import');
|
|
|
|
|
const { stdout: diffOutput } = await exec('yarn cs diff');
|
|
|
|
|
expect(diffOutput).toContain('No differences between DB and sync directory');
|
|
|
|
|
});
|
|
|
|
|
test('Import (update)', async () => {
|
|
|
|
|
// Update a core-store file.
|
|
|
|
|
await exec('sed -i \'s/"description":"",/"description":"test",/g\' config/sync/core-store.plugin_content_manager_configuration_content_types##plugin##users-permissions.user.json');
|
|
|
|
|
// Update a file that has relations.
|
|
|
|
|
await exec('sed -i \'s/{"action":"plugin::users-permissions.auth.register"},//g\' config/sync/user-role.public.json');
|
|
|
|
|
const { stdout: importOutput } = await exec('yarn cs import -y');
|
|
|
|
|
expect(importOutput).toContain('Finished import');
|
|
|
|
|
const { stdout: diffOutput } = await exec('yarn cs diff');
|
|
|
|
|
expect(diffOutput).toContain('No differences between DB and sync directory');
|
2021-12-31 13:28:04 +01:00
|
|
|
});
|
2023-10-22 12:36:28 +02:00
|
|
|
test('Import (create)', async () => {
|
|
|
|
|
// Add a file to trigger a creation.
|
|
|
|
|
await exec('mv .tmp/admin-role.strapi-editor.json config/sync/');
|
|
|
|
|
const { stdout: importOutput } = await exec('yarn cs import -y');
|
|
|
|
|
expect(importOutput).toContain('Finished import');
|
|
|
|
|
const { stdout: diffOutput } = await exec('yarn cs diff');
|
|
|
|
|
expect(diffOutput).toContain('No differences between DB and sync directory');
|
2021-12-31 13:28:04 +01:00
|
|
|
});
|
2023-10-22 12:36:28 +02:00
|
|
|
|
2026-10-11 13:35:21 +02:00
|
|
|
test('Import (unparsable file)', async () => {
|
|
|
|
|
// A file that can't be parsed (a merge conflict left in it, a typo) is not a missing file:
|
|
|
|
|
// importing it must fail, and leave the config in the database.
|
|
|
|
|
const file = 'config/sync/user-role.public.json';
|
|
|
|
|
const original = fs.readFileSync(file, 'utf8');
|
|
|
|
|
fs.writeFileSync(file, '<<<<<<< HEAD\n{ "name": "Public",\n');
|
|
|
|
|
let output;
|
|
|
|
|
try {
|
|
|
|
|
const { stdout, stderr } = await exec('yarn cs import -p user-role.public -y');
|
|
|
|
|
output = stdout + stderr;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
output = `${e.stdout}${e.stderr}`;
|
|
|
|
|
} finally {
|
|
|
|
|
fs.writeFileSync(file, original);
|
|
|
|
|
}
|
|
|
|
|
expect(output).toContain('is not valid JSON');
|
|
|
|
|
// The role is still in the database: with its file back, nothing differs.
|
|
|
|
|
const { stdout: diffOutput } = await exec('yarn cs diff');
|
|
|
|
|
expect(diffOutput).toContain('No differences between DB and sync directory');
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-03 19:45:03 +02:00
|
|
|
test('Non-empty diff returns 1', async () => {
|
2023-10-22 12:36:28 +02:00
|
|
|
await exec('rm -rf config/sync/admin-role.strapi-editor.json');
|
2022-09-03 19:45:03 +02:00
|
|
|
// Work around Jest not supporting custom error matching.
|
|
|
|
|
// https://github.com/facebook/jest/issues/8140
|
|
|
|
|
let error;
|
|
|
|
|
try {
|
|
|
|
|
await exec('yarn cs diff');
|
2023-10-19 08:19:06 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
2022-09-03 19:45:03 +02:00
|
|
|
}
|
|
|
|
|
expect(error).toHaveProperty('code', 1);
|
|
|
|
|
});
|
2025-06-28 17:37:38 +02:00
|
|
|
|
|
|
|
|
test('Import build project', async () => {
|
|
|
|
|
// First we make sure the dist folder is deleted.
|
2025-06-29 19:03:27 +02:00
|
|
|
await exec('mv dist .tmp');
|
2025-06-28 17:37:38 +02:00
|
|
|
|
|
|
|
|
await exec('yarn cs import -y');
|
|
|
|
|
|
|
|
|
|
const { stdout: buildOutput } = await exec('ls dist');
|
|
|
|
|
expect(buildOutput).toContain('config');
|
|
|
|
|
expect(buildOutput).toContain('src');
|
|
|
|
|
expect(buildOutput).toContain('tsconfig.tsbuildinfo');
|
|
|
|
|
|
2025-06-29 19:03:27 +02:00
|
|
|
// We restore the dist folder.
|
|
|
|
|
await exec('rm -rf dist');
|
|
|
|
|
await exec('mv .tmp/dist ./dist');
|
|
|
|
|
|
2025-06-28 17:37:38 +02:00
|
|
|
});
|
|
|
|
|
test('Import project already built', async () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// First we make sure the dist folder exists by doing an import.
|
|
|
|
|
await exec('yarn cs import -y');
|
|
|
|
|
|
|
|
|
|
const { stdout: lsDist } = await exec('ls dist');
|
|
|
|
|
expect(lsDist).toContain('config');
|
|
|
|
|
expect(lsDist).toContain('src');
|
|
|
|
|
expect(lsDist).toContain('tsconfig.tsbuildinfo');
|
|
|
|
|
|
|
|
|
|
// We remove on file from the dist folder
|
|
|
|
|
await exec('mv dist/tsconfig.tsbuildinfo .tmp');
|
|
|
|
|
|
|
|
|
|
// The new import should not rebuild the project.
|
|
|
|
|
await exec('yarn cs import -y');
|
|
|
|
|
|
|
|
|
|
const { stdout: buildOutput } = await exec('ls dist');
|
|
|
|
|
expect(buildOutput).toContain('config');
|
|
|
|
|
expect(buildOutput).toContain('src');
|
|
|
|
|
expect(buildOutput).not.toContain('tsconfig.tsbuildinfo');
|
|
|
|
|
|
2025-06-29 19:03:27 +02:00
|
|
|
// We restore the tsconfig.tsbuildinfo file.
|
|
|
|
|
await exec('mv .tmp/tsconfig.tsbuildinfo dist/tsconfig.tsbuildinfo');
|
|
|
|
|
|
2025-06-28 17:37:38 +02:00
|
|
|
});
|
2021-12-31 13:28:04 +01:00
|
|
|
});
|