Skip to content

Commit ac4e5b2

Browse files
authored
Merge pull request #24 from laiso/copilot/fix-eea916c3-79c8-454f-b335-042737f9e898
Unify benchmarkVersion with package.json version field
2 parents 910844c + 77acb81 commit ac4e5b2

File tree

6 files changed

+144
-7
lines changed

6 files changed

+144
-7
lines changed

package-lock.json

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "ts-bench",
3+
"version": "1.0.0",
34
"module": "index.ts",
45
"type": "module",
56
"private": true,

src/benchmark/__tests__/reporter.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ describe('BenchmarkReporter', () => {
8888
});
8989

9090
describe('exportToJSON', () => {
91-
it('generates basic JSON data structure', () => {
91+
it('generates basic JSON data structure', async () => {
9292
const privateReporter = reporter as any;
93-
const jsonData = privateReporter.generateBasicJSONData(mockResults, mockConfig);
93+
const jsonData = await privateReporter.generateBasicJSONData(mockResults, mockConfig);
9494

9595
expect(jsonData.metadata.agent).toBe('claude');
9696
expect(jsonData.metadata.model).toBe('sonnet-3.5');

src/benchmark/reporter.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { TestResult, BenchmarkConfig } from '../config/types';
22
import { formatDuration } from '../utils/duration';
3+
import { getPackageVersion } from '../utils/package-version';
34
import { writeFile, mkdir } from 'fs/promises';
45
import { join, dirname } from 'path';
56

@@ -57,7 +58,7 @@ export class BenchmarkReporter {
5758

5859
// JSON export functionality
5960
async exportToJSON(results: TestResult[], config: BenchmarkConfig, outputPath: string): Promise<void> {
60-
const data = this.generateBasicJSONData(results, config);
61+
const data = await this.generateBasicJSONData(results, config);
6162
await this.ensureDirectoryExists(outputPath);
6263
await writeFile(outputPath, JSON.stringify(data, null, 2), 'utf-8');
6364
console.log(`📄 Results exported to: ${outputPath}`);
@@ -85,7 +86,7 @@ export class BenchmarkReporter {
8586
}
8687

8788

88-
private generateBasicJSONData(results: TestResult[], config: BenchmarkConfig) {
89+
private async generateBasicJSONData(results: TestResult[], config: BenchmarkConfig) {
8990
const successCount = results.filter(r => r.overallSuccess).length;
9091
const totalCount = results.length;
9192
const successRate = (successCount / totalCount) * 100;
@@ -95,6 +96,8 @@ export class BenchmarkReporter {
9596
const testSuccessCount = results.filter(r => r.testSuccess).length;
9697
const testFailedCount = totalCount - testSuccessCount;
9798

99+
const benchmarkVersion = await getPackageVersion();
100+
98101
return {
99102
metadata: {
100103
timestamp: new Date().toISOString(),
@@ -103,7 +106,7 @@ export class BenchmarkReporter {
103106
provider: config.provider,
104107
version: config.version,
105108
totalExercises: totalCount,
106-
benchmarkVersion: "1.0.0"
109+
benchmarkVersion
107110
},
108111
summary: {
109112
successRate: Number(successRate.toFixed(1)),
@@ -127,6 +130,8 @@ export class BenchmarkReporter {
127130
const fileName = resultName || `${config.agent}-${config.model}-${config.provider}-${timestamp.replace(/[:.]/g, '-').slice(0, -5)}`;
128131
const fullPath = join(outputPath, `${fileName}.json`);
129132

133+
const benchmarkVersion = await getPackageVersion();
134+
130135
const data = {
131136
metadata: {
132137
agent: config.agent,
@@ -135,7 +140,7 @@ export class BenchmarkReporter {
135140
version: config.version,
136141
timestamp,
137142
exerciseCount: results.length,
138-
benchmarkVersion: "1.0.0",
143+
benchmarkVersion,
139144
generatedBy: "ts-bench"
140145
},
141146
summary: this.generateSummaryData(results),

src/utils/leaderboard-generator.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { TestResult, BenchmarkConfig } from '../config/types';
22
import type { AgentType } from '../config/types';
33
import { VersionDetector } from './version-detector';
4+
import { getPackageVersion } from './package-version';
45
import { readdir, readFile, writeFile, mkdir } from 'fs/promises';
56
import { join, dirname } from 'path';
67
import { existsSync } from 'fs';
@@ -242,11 +243,13 @@ export class LeaderboardGenerator {
242243
// Generate exercise breakdown
243244
const exerciseBreakdown = this.generateExerciseBreakdown(results);
244245

246+
const benchmarkVersion = await getPackageVersion();
247+
245248
return {
246249
metadata: {
247250
timestamp: new Date().toISOString(),
248251
totalExercises: results.length > 0 ? results[0]!.summary.totalCount : 0,
249-
benchmarkVersion: "1.0.0",
252+
benchmarkVersion,
250253
generatedBy: "ts-bench"
251254
},
252255
leaderboard: leaderboardEntries,

src/utils/package-version.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { readFile } from 'fs/promises';
2+
import { join, dirname } from 'path';
3+
import { fileURLToPath } from 'url';
4+
5+
/**
6+
* Get the version from package.json
7+
*/
8+
export async function getPackageVersion(): Promise<string> {
9+
try {
10+
// Get the path to the project root from the current module
11+
const currentDir = dirname(fileURLToPath(import.meta.url));
12+
// Navigate up to find package.json, checking multiple possible locations
13+
const possiblePaths = [
14+
join(currentDir, '../../package.json'), // From dist/utils/
15+
join(currentDir, '../../../package.json'), // From src/utils/
16+
join(process.cwd(), 'package.json') // From project root
17+
];
18+
19+
for (const packageJsonPath of possiblePaths) {
20+
try {
21+
const packageJsonContent = await readFile(packageJsonPath, 'utf-8');
22+
const packageJson = JSON.parse(packageJsonContent);
23+
24+
if (packageJson.version) {
25+
return packageJson.version;
26+
}
27+
} catch {
28+
// Try next path
29+
continue;
30+
}
31+
}
32+
33+
// If no package.json found, return fallback
34+
return '1.0.0';
35+
} catch (error) {
36+
console.warn('Failed to read package.json version, using fallback 1.0.0:', error);
37+
return '1.0.0';
38+
}
39+
}

0 commit comments

Comments
 (0)