|
1 | | -import { vol } from 'memfs'; |
| 1 | +import { writeFile } from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
2 | 3 | import { describe, expect, it } from 'vitest'; |
3 | 4 | import { commitSchema } from '@code-pushup/models'; |
4 | | -import { MEMFS_VOLUME, MINIMAL_CONFIG_MOCK } from '@code-pushup/test-utils'; |
| 5 | +import { MINIMAL_CONFIG_MOCK, cleanTestFolder } from '@code-pushup/test-utils'; |
| 6 | +import { |
| 7 | + ensureDirectoryExists, |
| 8 | + fileExists, |
| 9 | + readJsonFile, |
| 10 | +} from '@code-pushup/utils'; |
5 | 11 | import { collect } from './collect.js'; |
| 12 | +import { getRunnerOutputsPath } from './runner.js'; |
6 | 13 |
|
7 | 14 | describe('collect', () => { |
| 15 | + const outputDir = path.join('tmp', 'int', 'core', 'collect', '.code-pushup'); |
| 16 | + |
| 17 | + const expectedCachedOutput = [ |
| 18 | + { |
| 19 | + slug: 'node-version', |
| 20 | + score: 0.3, |
| 21 | + value: 16, |
| 22 | + displayValue: '16.0.0', |
| 23 | + details: { |
| 24 | + issues: [ |
| 25 | + { |
| 26 | + severity: 'error', |
| 27 | + message: 'The required Node version to run Code PushUp CLI is 18.', |
| 28 | + }, |
| 29 | + ], |
| 30 | + }, |
| 31 | + }, |
| 32 | + ]; |
| 33 | + |
| 34 | + const expectedCachedTestData = [ |
| 35 | + { |
| 36 | + slug: 'node-version', |
| 37 | + score: 0.8, |
| 38 | + value: 18, |
| 39 | + displayValue: '18.0.0', |
| 40 | + details: { issues: [] }, |
| 41 | + }, |
| 42 | + ]; |
| 43 | + |
| 44 | + const expectedPluginOutput = { |
| 45 | + slug: 'node', |
| 46 | + title: 'Node', |
| 47 | + icon: 'javascript', |
| 48 | + date: expect.any(String), |
| 49 | + audits: expectedCachedOutput.map(audit => ({ |
| 50 | + ...audit, |
| 51 | + title: 'Node version', |
| 52 | + description: 'Returns node version', |
| 53 | + docsUrl: 'https://nodejs.org/', |
| 54 | + })), |
| 55 | + }; |
| 56 | + |
| 57 | + beforeEach(async () => { |
| 58 | + await cleanTestFolder(outputDir); |
| 59 | + await ensureDirectoryExists(outputDir); |
| 60 | + }); |
| 61 | + |
8 | 62 | it('should execute with valid options', async () => { |
9 | | - vol.fromJSON({}, MEMFS_VOLUME); |
10 | 63 | const report = await collect({ |
11 | 64 | ...MINIMAL_CONFIG_MOCK, |
12 | | - verbose: true, |
| 65 | + persist: { outputDir }, |
| 66 | + cache: { read: false, write: false }, |
13 | 67 | progress: false, |
14 | 68 | }); |
15 | 69 |
|
16 | | - expect(report.plugins[0]?.audits[0]).toEqual( |
17 | | - expect.objectContaining({ |
18 | | - slug: 'node-version', |
19 | | - displayValue: '16.0.0', |
20 | | - details: { |
21 | | - issues: [ |
22 | | - { |
23 | | - severity: 'error', |
24 | | - message: |
25 | | - 'The required Node version to run Code PushUp CLI is 18.', |
26 | | - }, |
27 | | - ], |
28 | | - }, |
29 | | - }), |
30 | | - ); |
| 70 | + expect(report.plugins[0]).toStrictEqual({ |
| 71 | + ...expectedPluginOutput, |
| 72 | + duration: expect.any(Number), |
| 73 | + }); |
| 74 | + expect(report.plugins[0]?.duration).toBeGreaterThanOrEqual(0); |
31 | 75 |
|
32 | 76 | expect(() => commitSchema.parse(report.commit)).not.toThrow(); |
| 77 | + |
| 78 | + await expect( |
| 79 | + fileExists(getRunnerOutputsPath('node', outputDir)), |
| 80 | + ).resolves.toBeFalsy(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should write runner outputs with --cache.write option', async () => { |
| 84 | + const report = await collect({ |
| 85 | + ...MINIMAL_CONFIG_MOCK, |
| 86 | + persist: { outputDir }, |
| 87 | + cache: { read: false, write: true }, |
| 88 | + progress: false, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(report.plugins[0]).toStrictEqual({ |
| 92 | + ...expectedPluginOutput, |
| 93 | + duration: expect.any(Number), |
| 94 | + }); |
| 95 | + expect(report.plugins[0]?.duration).toBeGreaterThanOrEqual(0); |
| 96 | + |
| 97 | + await expect( |
| 98 | + readJsonFile(getRunnerOutputsPath('node', outputDir)), |
| 99 | + ).resolves.toStrictEqual(expectedCachedOutput); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should read runner outputs with --cache.read option and have plugin duraton 0', async () => { |
| 103 | + const cacheFilePath = getRunnerOutputsPath('node', outputDir); |
| 104 | + await ensureDirectoryExists(path.dirname(cacheFilePath)); |
| 105 | + await writeFile(cacheFilePath, JSON.stringify(expectedCachedTestData)); |
| 106 | + |
| 107 | + const report = await collect({ |
| 108 | + ...MINIMAL_CONFIG_MOCK, |
| 109 | + persist: { outputDir }, |
| 110 | + cache: { read: true, write: false }, |
| 111 | + progress: false, |
| 112 | + }); |
| 113 | + |
| 114 | + expect(report.plugins[0]?.audits[0]).toStrictEqual( |
| 115 | + expect.objectContaining(expectedCachedTestData[0]), |
| 116 | + ); |
| 117 | + |
| 118 | + expect(report.plugins[0]).toStrictEqual({ |
| 119 | + ...expectedPluginOutput, |
| 120 | + duration: 0, |
| 121 | + audits: expect.any(Array), |
| 122 | + }); |
| 123 | + |
| 124 | + await expect(readJsonFile(cacheFilePath)).resolves.toStrictEqual( |
| 125 | + expectedCachedTestData, |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should execute runner and write cache with --cache option', async () => { |
| 130 | + const report = await collect({ |
| 131 | + ...MINIMAL_CONFIG_MOCK, |
| 132 | + persist: { outputDir }, |
| 133 | + cache: { read: true, write: true }, |
| 134 | + progress: false, |
| 135 | + }); |
| 136 | + |
| 137 | + expect(report.plugins[0]).toStrictEqual({ |
| 138 | + ...expectedPluginOutput, |
| 139 | + duration: expect.any(Number), |
| 140 | + }); |
| 141 | + expect(report.plugins[0]?.duration).toBeGreaterThanOrEqual(0); |
| 142 | + |
| 143 | + await expect( |
| 144 | + readJsonFile(getRunnerOutputsPath('node', outputDir)), |
| 145 | + ).resolves.toStrictEqual(expectedCachedOutput); |
33 | 146 | }); |
34 | 147 | }); |
0 commit comments