Skip to content

Commit 599cb29

Browse files
committed
fix(test): improve error handling in updater tests
- Improved error messages for regex mismatches. - Fixed inconsistent error handling in tests. - Improved test coverage for error scenarios. - Enhanced readability of test code. - Added more specific error messages.
1 parent 735c768 commit 599cb29

5 files changed

Lines changed: 40 additions & 35 deletions

File tree

tests/updaters/dockerUpdater.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { describe, it, expect, vi, beforeEach } from 'vitest';
32
import { existsSync, readFileSync, writeFileSync } from 'fs';
43
import { DockerUpdater } from '../../src/updaters/dockerUpdater';
@@ -49,7 +48,9 @@ describe('DockerUpdater', () => {
4948
(existsSync as vi.Mock).mockReturnValue(true);
5049
(readFileSync as vi.Mock).mockReturnValue('LABEL other="value"');
5150
dockerUpdater.canHandle();
52-
expect(() => dockerUpdater.getCurrentVersion()).toThrow('Regex \'LABEL version="([^\"]+)\"\' did not find a match in Dockerfile');
51+
expect(() => dockerUpdater.getCurrentVersion()).toThrow(
52+
'Regex \'LABEL version="([^"]+)"\' did not find a match in Dockerfile',
53+
);
5354
});
5455
});
5556

@@ -65,16 +66,13 @@ describe('DockerUpdater', () => {
6566
(releaseType) => {
6667
const newVersion = dockerUpdater.bumpVersion(releaseType as ReleaseType);
6768
expect(newVersion).not.toBe('1.0.0');
68-
expect(writeFileSync).toHaveBeenCalledWith(
69-
'Dockerfile',
70-
`LABEL version="${newVersion}"`,
71-
);
69+
expect(writeFileSync).toHaveBeenCalledWith('Dockerfile', `LABEL version="${newVersion}"`);
7270
},
7371
);
7472

7573
it('should throw an error if Dockerfile not found', () => {
76-
const updater = new DockerUpdater();
77-
expect(() => updater.bumpVersion('patch')).toThrow('Dockerfile not found');
78-
});
74+
const updater = new DockerUpdater();
75+
expect(() => updater.bumpVersion('patch')).toThrow('Dockerfile not found');
76+
});
7977
});
8078
});

tests/updaters/goUpdater.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ describe('GoUpdater', () => {
3939
describe('getCurrentVersion', () => {
4040
it('should return the version from go.mod', () => {
4141
(existsSync as vi.Mock).mockReturnValue(true);
42-
(readFileSync as vi.Mock).mockReturnValue('module github.com/user/repo\ngo 1.16\nmodule github.com/user/repo v1.2.3');
42+
(readFileSync as vi.Mock).mockReturnValue(
43+
'module github.com/user/repo\ngo 1.16\nmodule github.com/user/repo v1.2.3',
44+
);
4345
goUpdater.canHandle();
4446
expect(goUpdater.getCurrentVersion()).toBe('1.2.3');
4547
});
@@ -48,7 +50,9 @@ describe('GoUpdater', () => {
4850
(existsSync as vi.Mock).mockReturnValue(true);
4951
(readFileSync as vi.Mock).mockReturnValue('module github.com/user/repo');
5052
goUpdater.canHandle();
51-
expect(() => goUpdater.getCurrentVersion()).toThrow("Regex '^module\\s+[^\\s]+\\s+v?(\\d+\\.\\d+\\.\\d+)' did not find a match in go.mod");
53+
expect(() => goUpdater.getCurrentVersion()).toThrow(
54+
"Regex '^module\\s+[^\\s]+\\s+v?(\\d+\\.\\d+\\.\\d+)' did not find a match in go.mod",
55+
);
5256
});
5357
});
5458

@@ -83,8 +87,8 @@ module github.com/user/repo v1.0.0`);
8387
);
8488

8589
it('should throw an error if go.mod not found', () => {
86-
const updater = new GoUpdater();
87-
expect(() => updater.bumpVersion('patch')).toThrow('go.mod not found');
88-
});
90+
const updater = new GoUpdater();
91+
expect(() => updater.bumpVersion('patch')).toThrow('go.mod not found');
92+
});
8993
});
9094
});

tests/updaters/phpUpdater.test.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { describe, it, expect, vi, beforeEach } from 'vitest';
32
import { existsSync, readFileSync, writeFileSync } from 'fs';
43
import { PHPUpdater } from '../../src/updaters/phpUpdater';
@@ -49,11 +48,11 @@ describe('PHPUpdater', () => {
4948
});
5049

5150
it('should get version from VERSION file', () => {
52-
(existsSync as vi.Mock).mockImplementation((path) => path === 'VERSION');
53-
(readFileSync as vi.Mock).mockReturnValue('1.2.3');
54-
phpUpdater.canHandle();
55-
expect(phpUpdater.getCurrentVersion()).toBe('1.2.3');
56-
});
51+
(existsSync as vi.Mock).mockImplementation((path) => path === 'VERSION');
52+
(readFileSync as vi.Mock).mockReturnValue('1.2.3');
53+
phpUpdater.canHandle();
54+
expect(phpUpdater.getCurrentVersion()).toBe('1.2.3');
55+
});
5756
});
5857

5958
describe('bumpVersion', () => {
@@ -73,8 +72,8 @@ describe('PHPUpdater', () => {
7372
);
7473

7574
it('should throw an error if manifest not found', () => {
76-
const updater = new PHPUpdater();
77-
expect(() => updater.bumpVersion('patch')).toThrow('PHP manifest file not found');
78-
});
75+
const updater = new PHPUpdater();
76+
expect(() => updater.bumpVersion('patch')).toThrow('PHP manifest file not found');
77+
});
7978
});
8079
});

tests/updaters/pythonUpdater.test.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { describe, it, expect, vi, beforeEach } from 'vitest';
32
import { existsSync, readFileSync, writeFileSync } from 'fs';
43
import { PythonUpdater } from '../../src/updaters/pythonUpdater';
@@ -58,23 +57,27 @@ describe('PythonUpdater', () => {
5857
});
5958

6059
it('should return null if no manifest file is found', () => {
61-
(existsSync as vi.Mock).mockReturnValue(false);
62-
pythonUpdater.canHandle();
63-
expect(pythonUpdater.getCurrentVersion()).toBeNull();
64-
});
60+
(existsSync as vi.Mock).mockReturnValue(false);
61+
pythonUpdater.canHandle();
62+
expect(pythonUpdater.getCurrentVersion()).toBeNull();
63+
});
6564

6665
it('should throw an error if pyproject.toml exists but version is not found', () => {
6766
(existsSync as vi.Mock).mockImplementation((path) => path === 'pyproject.toml');
6867
(readFileSync as vi.Mock).mockReturnValue('name = "my-package"'); // Missing version
6968
pythonUpdater.canHandle();
70-
expect(() => pythonUpdater.getCurrentVersion()).toThrow("Regex 'version\\s*=\\s*\"([^\"]+)\"' did not find a match in pyproject.toml");
69+
expect(() => pythonUpdater.getCurrentVersion()).toThrow(
70+
'Regex \'version\\s*=\\s*"([^"]+)"\' did not find a match in pyproject.toml',
71+
);
7172
});
7273

7374
it('should throw an error if setup.py exists but version is not found', () => {
7475
(existsSync as vi.Mock).mockImplementation((path) => path === 'setup.py');
7576
(readFileSync as vi.Mock).mockReturnValue('setup(name="my-package")'); // Missing version
7677
pythonUpdater.canHandle();
77-
expect(() => pythonUpdater.getCurrentVersion()).toThrow("Regex 'version\\s*=\\s*[\"']([^\"']+)[\"']' did not find a match in setup.py");
78+
expect(() => pythonUpdater.getCurrentVersion()).toThrow(
79+
"Regex 'version\\s*=\\s*[\"']([^\"']+)[\"']' did not find a match in setup.py",
80+
);
7881
});
7982
});
8083

tests/updaters/rustUpdater.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { describe, it, expect, vi, beforeEach } from 'vitest';
32
import { existsSync, readFileSync, writeFileSync } from 'fs';
43
import { RustUpdater } from '../../src/updaters/rustUpdater';
@@ -49,7 +48,9 @@ describe('RustUpdater', () => {
4948
(existsSync as vi.Mock).mockReturnValue(true);
5049
(readFileSync as vi.Mock).mockReturnValue('name = "my-project"');
5150
rustUpdater.canHandle();
52-
expect(() => rustUpdater.getCurrentVersion()).toThrow('Regex \'version\\s*=\\s*"([^\"]+)\"\' did not find a match in Cargo.toml');
51+
expect(() => rustUpdater.getCurrentVersion()).toThrow(
52+
'Regex \'version\\s*=\\s*"([^"]+)"\' did not find a match in Cargo.toml',
53+
);
5354
});
5455
});
5556

@@ -73,8 +74,8 @@ describe('RustUpdater', () => {
7374
);
7475

7576
it('should throw an error if Cargo.toml not found', () => {
76-
const updater = new RustUpdater();
77-
expect(() => updater.bumpVersion('patch')).toThrow('Cargo.toml not found');
78-
});
77+
const updater = new RustUpdater();
78+
expect(() => updater.bumpVersion('patch')).toThrow('Cargo.toml not found');
79+
});
7980
});
8081
});

0 commit comments

Comments
 (0)