Skip to content

Commit bd37aaf

Browse files
committed
Add CI tests with Markdown output
1 parent 54aea99 commit bd37aaf

File tree

4 files changed

+118
-13
lines changed

4 files changed

+118
-13
lines changed

.github/workflows/update-tests-md.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Update Tests Results
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- test-workflow
8+
pull_request:
9+
branches:
10+
- main
11+
- test-workflow
12+
workflow_dispatch:
13+
14+
jobs:
15+
run_tests_and_update_markdown:
16+
runs-on: ubuntu-latest
17+
18+
env:
19+
E2B_API_KEY: ${{ secrets.E2B_API_KEY }}
20+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
21+
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
22+
MISTRAL_API_KEY: ${{ secrets.MISTRAL_API_KEY }}
23+
FIREWORKS_API_KEY: ${{ secrets.FIREWORKS_API_KEY }}
24+
FIRECRAWL_API_KEY: ${{ secrets.FIRECRAWL_API_KEY }}
25+
GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }}
26+
TOGETHER_API_KEY: ${{ secrets.TOGETHER_API_KEY }}
27+
28+
steps:
29+
- name: Checkout code
30+
uses: actions/checkout@v2
31+
32+
- name: Set up Node.js
33+
uses: actions/setup-node@v2
34+
with:
35+
node-version: '20'
36+
37+
- name: Install dependencies
38+
run: npm install
39+
40+
- name: Run Jest tests and save results
41+
run: |
42+
npm test -- --json --outputFile=./results.json
43+
continue-on-error: true # Ensures the job continues even if tests fail
44+
45+
- name: Upload test results as an artifact
46+
uses: actions/upload-artifact@v3
47+
with:
48+
name: test-results
49+
path: ./results.json
50+
51+
- name: Update Tests.md with test results
52+
run: node ./tests/updateTestsMd.js # Your custom script to update Tests.md with test results
53+
54+
- name: Upload markdown report as an artifact
55+
uses: actions/upload-artifact@v3
56+
with:
57+
name: test-results-md
58+
path: ./Tests.md

tests/examples.test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path';
33
import dotenv from 'dotenv';
44
import Sandbox from 'e2b';
55

6-
import { uploadPathToPath, readEnvFile } from "./utils"
6+
import { uploadPathToPath, getApiKeys } from "./utils"
77

88
// Read the E2B API key
99
dotenv.config({ path: path.resolve(process.cwd(), ".env") });
@@ -95,7 +95,7 @@ describe('Integration test for multiple scripts in e2b sandbox', () => {
9595
});
9696

9797
scripts.forEach(({ name, interpreter, file : examplePath }) => {
98-
it.concurrent(`should upload and execute ${name} successfully in e2b sandbox`, async () => {
98+
it.concurrent(name, async () => {
9999

100100
let attempts = 0;
101101
const maxAttempts = 3;
@@ -104,9 +104,10 @@ describe('Integration test for multiple scripts in e2b sandbox', () => {
104104
while (attempts < maxAttempts && !success) {
105105
attempts++;
106106

107+
// Create a new E2B sandbox
108+
const sandbox = await Sandbox.create({ timeoutMs: SANDBOX_TIMEOUT });
109+
107110
try {
108-
// Create a new E2B sandbox
109-
const sandbox = await Sandbox.create({ timeoutMs: SANDBOX_TIMEOUT });
110111

111112
// Upload the example directory to the sandbox.
112113
await uploadPathToPath(examplePath, SANDBOX_TEST_DIRECTORY, sandbox);
@@ -132,13 +133,10 @@ describe('Integration test for multiple scripts in e2b sandbox', () => {
132133
stdoutData += output;
133134
await fs.appendFile(logFilePath, output);
134135
},
135-
envs: readEnvFile(),
136+
envs: getApiKeys(),
136137
timeoutMs: COMMAND_TIMEOUT,
137138
});
138139

139-
// Kill the sandbox
140-
await sandbox.kill();
141-
142140
// Check the exit code to see if the test passed
143141
if (result.exitCode !== 0) {
144142
await fs.appendFile(logFilePath, `Attempt ${attempts}: Test for ${name} failed with exit code ${result.exitCode}\n`);
@@ -156,7 +154,10 @@ describe('Integration test for multiple scripts in e2b sandbox', () => {
156154
await fs.appendFile(logFilePath, `Test for ${name} completed successfully on attempt ${attempts}.\n`);
157155
}
158156
} catch (error) {
159-
console.log(`Attempt ${attempts}/${maxAttempts}: An error occurred while running the test for ${name}`);
157+
console.log(`Attempt ${attempts}/${maxAttempts}: An error occurred while running the test for ${name}`, error);
158+
} finally {
159+
// Kill the sandbox
160+
await sandbox.kill();
160161
}
161162

162163
if (!success && attempts === maxAttempts) {

tests/updateTestsMd.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { promises as fs } from 'fs';
2+
import path from 'path';
3+
import { fileURLToPath } from 'url';
4+
5+
// Get the current directory name
6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = path.dirname(__filename);
8+
9+
// Define paths for the results and markdown files
10+
const resultsPath = path.join(__dirname, 'results.json');
11+
const markdownPath = path.join(__dirname, 'Tests.md');
12+
13+
// Read the Jest results
14+
fs.readFile(resultsPath, 'utf8')
15+
.then((data) => {
16+
const results = JSON.parse(data);
17+
18+
// Start constructing the Markdown table
19+
let markdownContent = `# Test Results\n\n`;
20+
markdownContent += `| Test Name | Status |\n`;
21+
markdownContent += `|-----------|--------|\n`;
22+
23+
// Iterate through each test result and populate the Markdown table
24+
results.testResults.forEach((test) => {
25+
const testName = test.assertionResults[0].fullName;
26+
const status = test.status === 'passed' ? '✅ Passed' : '❌ Failed';
27+
markdownContent += `| ${testName} | ${status} |\n`;
28+
});
29+
30+
// Write the Markdown content to Tests.md
31+
console.log(markdownContent)
32+
return fs.writeFile(markdownPath, markdownContent);
33+
})
34+
.then(() => {
35+
console.log('Tests.md updated successfully!');
36+
})
37+
.catch((err) => {
38+
console.error('Error processing test results:', err);
39+
});

tests/utils.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@ import path from 'path';
33
import dotenv from 'dotenv';
44
import ignore, { Ignore } from 'ignore';
55

6-
// Read and parse a .env file
7-
export function readEnvFile(filePath: string = '.env'): Record<string, string> {
8-
const envPath = path.resolve(process.cwd(), filePath);
9-
return dotenv.parse(readFileSync(envPath, 'utf-8'));
6+
export function getApiKeys(): Record<string, string> {
7+
return {
8+
E2B_API_KEY: process.env.E2B_API_KEY || '',
9+
OPENAI_API_KEY: process.env.OPENAI_API_KEY || '',
10+
ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY || '',
11+
MISTRAL_API_KEY: process.env.MISTRAL_API_KEY || '',
12+
FIREWORKS_API_KEY: process.env.FIREWORKS_API_KEY || '',
13+
FIRECRAWL_API_KEY: process.env.FIRECRAWL_API_KEY || '',
14+
GROQ_API_KEY: process.env.GROQ_API_KEY || '',
15+
TOGETHER_API_KEY: process.env.TOGETHER_API_KEY || '',
16+
};
1017
}
1118

1219
// Read and parse a .gitignore file

0 commit comments

Comments
 (0)