Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core-open/scanners/rust-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,8 @@ function parseCargoToml(
* edition = "2021"
*/
function extractRustEdition(content: string): string | undefined {
const match = content.match(/^\s*edition\s*=\s*"(\d+)"/m);
return match?.[1];
const match = content.match(/^\s*edition\s*=\s*(["'])(\d+)\1/m);
return match?.[2];
}

// ── Rust project file names ──
Expand Down
51 changes: 51 additions & 0 deletions test/cargo-rust-edition.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Cargo.toml package edition. TOML basic ("2021") and literal ('2021')
* strings are both valid; the scanner must report the same edition and lag
* for either quote style.
*/
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { describe, expect, it } from 'vitest';
import { CargoCache } from '../src/core-open/scanners/cargo-cache.js';
import { scanRustProjects } from '../src/core-open/scanners/rust-scanner.js';
import { Semaphore } from '../src/core-open/utils/semaphore.js';
import type { ProjectScan } from '../src/core-open/types.js';

async function scanEdition(editionLine: string): Promise<ProjectScan> {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'vg-cargo-edition-'));
try {
fs.writeFileSync(
path.join(root, 'Cargo.toml'),
`[package]\nname = "app"\nversion = "0.1.0"\n${editionLine}\n`,
);
const projects = await scanRustProjects(root, new CargoCache(new Semaphore(1), undefined, true));
expect(projects).toHaveLength(1);
return projects[0]!;
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
}

describe('Cargo package edition', () => {
it('reads a double-quoted edition', async () => {
const project = await scanEdition('edition = "2021"');

expect(project.runtime).toBe('2021');
expect(project.runtimeLatest).toBe('2024');
expect(project.runtimeMajorsBehind).toBe(1);
});

it('reads a single-quoted edition with the same lag as the double-quoted form', async () => {
const project = await scanEdition("edition = '2021'");

expect(project.runtime).toBe('2021');
expect(project.runtimeLatest).toBe('2024');
expect(project.runtimeMajorsBehind).toBe(1);
});

it('ignores a quoted edition that is not a year of digits', async () => {
expect((await scanEdition("edition = 'latest'")).runtime).toBeUndefined();
expect((await scanEdition('edition = "latest"')).runtime).toBeUndefined();
});
});