Skip to content

Commit 8fe80d8

Browse files
committed
feat(test): write test logs to timestamped files in special dir
1 parent 9f9fe30 commit 8fe80d8

File tree

5 files changed

+75
-7
lines changed

5 files changed

+75
-7
lines changed

.github/workflows/release.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ jobs:
5757
run: npm run build
5858
- name: Test
5959
run: npm run test
60+
- name: Upload test logs
61+
uses: actions/upload-artifact@v2
62+
if: always()
63+
with:
64+
name: test-logs
65+
path: .test-logs/
6066
- name: Verify the integrity of provenance attestations and registry signatures for installed dependencies
6167
run: npm audit signatures
6268
- name: Release

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
*.js
55
*.js.map
66

7+
# Test logs
8+
test-logs/**/*
9+
710
# Files tagged with .ignore
811
*.ignore
912

test/objects/relationship.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, beforeEach, it, expect } from 'vitest';
1+
import { describe, beforeEach, it, expect, afterAll } from 'vitest';
22
import { z } from 'zod';
33
import { v4 as uuidv4 } from 'uuid';
44
import {
@@ -21,6 +21,8 @@ import {
2121
stixTypeSchema,
2222
xMitreIdentity,
2323
} from '../../src/schemas/common/index';
24+
import { logger } from '../utils/logger';
25+
2426

2527
describe('RelationshipSchema', () => {
2628
let relationships: any[];
@@ -327,12 +329,12 @@ describe('RelationshipSchema', () => {
327329
};
328330

329331
if (errors.length > 0) {
330-
console.warn(
332+
logger.warn(
331333
`The following ${errors.length} relationship(s) failed validation:\n${formatErrorReport(errors)}`,
332334
);
333335
}
334336

335-
console.log(`
337+
logger.log(`
336338
Total relationships: ${relationships.length}
337339
Valid relationships: ${validRelationships.length}
338340
Relationships with errors: ${errors.length}
@@ -341,4 +343,8 @@ describe('RelationshipSchema', () => {
341343
// This expectation will always pass, but it gives us a way to surface the error count in the test results
342344
expect(true).toBe(true);
343345
});
346+
347+
afterAll(() => {
348+
logger.close();
349+
});
344350
});

test/objects/stix-bundle.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, beforeEach, it, expect } from 'vitest';
1+
import { describe, beforeEach, it, expect, afterAll } from 'vitest';
22
import { v4 as uuidv4 } from 'uuid';
33
import { type StixBundle, stixBundleSchema } from '../../src/schemas/sdo/stix-bundle.schema';
44
import {
@@ -9,6 +9,7 @@ import {
99
import type { Collection } from '../../src/schemas/sdo/collection.schema';
1010
import type { Technique } from '../../src/schemas/sdo/technique.schema';
1111
import { z } from 'zod';
12+
import { logger } from '../utils/logger';
1213

1314
/**
1415
* Test suite for validating StixBundle schema.
@@ -220,16 +221,21 @@ describe('StixBundleSchema', () => {
220221
});
221222

222223
bundlesWithErrors.push({ bundleIndex, errors });
223-
console.warn(errors.join('\n\n'));
224+
logger.warn(errors.join('\n\n'));
224225
}
225226
}
226227
});
227228

228229
// Log a summary of the validation results
229-
console.log(`Validated ${bundles.length} bundles`);
230-
console.log(`Found errors in ${bundlesWithErrors.length} bundles`);
230+
logger.log(`Validated ${bundles.length} bundles`);
231+
logger.log(`Found errors in ${bundlesWithErrors.length} bundles`);
231232

232233
// This expectation will always pass, but it gives us a way to surface the error count in the test results
233234
expect(bundlesWithErrors.length).toBeLessThanOrEqual(bundles.length);
234235
});
236+
237+
// Close the logger after all tests are complete
238+
afterAll(() => {
239+
logger.close();
240+
});
235241
});

test/utils/logger.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// file: test/utils/logger.ts
2+
3+
import fs from 'fs';
4+
import path from 'path';
5+
6+
class TestLogger {
7+
private static instance: TestLogger;
8+
private logStream: fs.WriteStream;
9+
private static readonly LOG_DIR = '.test-logs';
10+
11+
private constructor() {
12+
const timestamp = new Date().toISOString().replace(/[:]/g, '-').replace(/\..+/, '');
13+
14+
// Ensure the log directory exists
15+
if (!fs.existsSync(TestLogger.LOG_DIR)) {
16+
fs.mkdirSync(TestLogger.LOG_DIR, { recursive: true });
17+
}
18+
19+
const logFilePath = path.join(TestLogger.LOG_DIR, `test-log-${timestamp}.txt`);
20+
this.logStream = fs.createWriteStream(logFilePath, { flags: 'a' });
21+
}
22+
23+
public static getInstance(): TestLogger {
24+
if (!TestLogger.instance) {
25+
TestLogger.instance = new TestLogger();
26+
}
27+
return TestLogger.instance;
28+
}
29+
30+
public log(message: string): void {
31+
this.logStream.write(`LOG: ${message}\n`);
32+
}
33+
34+
public warn(message: string): void {
35+
this.logStream.write(`WARNING: ${message}\n`);
36+
}
37+
38+
public error(message: string): void {
39+
this.logStream.write(`ERROR: ${message}\n`);
40+
}
41+
42+
public close(): void {
43+
this.logStream.end();
44+
}
45+
}
46+
47+
export const logger = TestLogger.getInstance();

0 commit comments

Comments
 (0)