From 543d029baf7083b57fc461b96324c568ac2acb84 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Thu, 6 Jan 2022 17:26:41 +0300 Subject: [PATCH] Create commands/create-matrix.js --- scripts/commands/create-matrix.js | 15 +++++++++++++++ tests/commands/create-matrix.test.js | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 scripts/commands/create-matrix.js create mode 100644 tests/commands/create-matrix.test.js diff --git a/scripts/commands/create-matrix.js b/scripts/commands/create-matrix.js new file mode 100644 index 00000000..be639b02 --- /dev/null +++ b/scripts/commands/create-matrix.js @@ -0,0 +1,15 @@ +const { logger, db } = require('../core') + +async function main() { + const docs = await db.find({}).sort({ cluster_id: 1 }) + const cluster_id = docs.reduce((acc, curr) => { + if (!acc.includes(curr.cluster_id)) acc.push(curr.cluster_id) + return acc + }, []) + + const matrix = { cluster_id } + const output = `::set-output name=matrix::${JSON.stringify(matrix)}` + logger.info(output) +} + +main() diff --git a/tests/commands/create-matrix.test.js b/tests/commands/create-matrix.test.js new file mode 100644 index 00000000..a8877cce --- /dev/null +++ b/tests/commands/create-matrix.test.js @@ -0,0 +1,20 @@ +const fs = require('fs') +const path = require('path') +const { execSync } = require('child_process') + +beforeEach(() => { + fs.copyFileSync('tests/__data__/input/test.db', 'tests/__data__/temp/test.db') +}) + +afterEach(() => { + fs.rmdirSync('tests/__data__/temp', { recursive: true }) + fs.mkdirSync('tests/__data__/temp') +}) + +it('can create valid matrix', () => { + const result = execSync( + 'DB_FILEPATH=tests/__data__/temp/test.db node scripts/commands/create-matrix.js', + { encoding: 'utf8' } + ) + expect(result).toBe('::set-output name=matrix::{"cluster_id":[1]}\n') +})