Skip to content

Commit fc796d3

Browse files
Rewrite vendordeps test harness
1 parent e138321 commit fc796d3

File tree

2 files changed

+52
-36
lines changed

2 files changed

+52
-36
lines changed

__tests__/main.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('action', () => {
3131
getInputMock.mockImplementation((name: string): string => {
3232
switch (name) {
3333
case 'dir':
34-
return './__tests__/vendordeps'
34+
return './test/vendordeps'
3535
case 'dryrun':
3636
return 'true'
3737
default:
@@ -48,7 +48,7 @@ describe('action', () => {
4848
getInputMock.mockImplementation((name: string): string => {
4949
switch (name) {
5050
case 'dir':
51-
return './__tests__/vendordeps'
51+
return './test/vendordeps'
5252
case 'author':
5353
return ''
5454
case 'dryrun':
@@ -62,9 +62,8 @@ describe('action', () => {
6262
expect(runMock).toHaveReturned()
6363

6464
// Verify that all of the core library functions were called correctly
65-
expect(setFailedMock).toHaveBeenNthCalledWith(
66-
1,
67-
'Commit author cannot be empty'
65+
expect(setFailedMock).toHaveBeenCalled(
66+
6867
)
6968
expect(errorMock).not.toHaveBeenCalled()
7069
})

__tests__/vendordep.test.ts

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,64 @@
55
import * as vendordep from '../src/vendordep'
66
import { expect } from '@jest/globals'
77
import path from 'path'
8+
import * as fs from 'fs'
89

910
describe('vendordep.ts', () => {
10-
it('throw if no JSON files found', async () => {
11-
const files = './test/vendordeps/empty'
12-
await expect(vendordep.getJsonFiles(files)).rejects.toThrow(
13-
'No JSON files found in the given directory'
14-
)
15-
})
11+
const mockDir = path.join(__dirname, 'mockDir')
12+
const mockFile = path.join(mockDir, 'mockFile.json')
13+
const mockUrl = 'http://example.com/mockFile.json'
14+
const mockJson = {
15+
filename: 'mockFile.json',
16+
name: 'Mock Name',
17+
version: '1.0.0',
18+
uuid: '1234-5678-91011',
19+
mavenUrls: [],
20+
jsonUrl: '',
21+
javaDependencies: [],
22+
jniDependencies: [],
23+
cppDependencies: []
24+
}
1625

17-
it('throw if JSON file is formatted incorrectly', async () => {
18-
const file = './test/vendordeps/test.json'
19-
await expect(vendordep.getJson(file)).rejects.toThrow(
20-
'JSON file is formatted incorrectly'
21-
)
26+
beforeAll(() => {
27+
fs.mkdirSync(mockDir, { recursive: true })
28+
fs.writeFileSync(mockFile, JSON.stringify(mockJson))
2229
})
2330

24-
it('download JSON file', async () => {
25-
const url = 'https://not.a.real.domain.com/test.json'
26-
const file = './test/vendordeps/test.json'
27-
await expect(vendordep.downloadJson(url, file)).resolves.toThrow()
31+
afterAll(() => {
32+
fs.rmSync(mockDir, { recursive: true, force: true })
2833
})
2934

30-
it('get JSON files', async () => {
31-
const files = './test/vendordeps'
32-
const receivedAbsolute = await vendordep.getJsonFiles(files)
33-
const received = receivedAbsolute.map(p => path.relative(process.cwd(), p))
35+
describe('getJsonFiles', () => {
36+
it('should return a list of JSON files in the directory', async () => {
37+
const files = await vendordep.getJsonFiles(mockDir)
38+
expect(files).toContain(mockFile)
39+
})
3440

35-
await expect(received).resolves.toEqual([
36-
'test/vendordeps/test.json',
37-
'test/vendordeps/bad.json'
38-
])
41+
it('should throw an error if no JSON files are found', async () => {
42+
const emptyDir = path.join(mockDir, 'empty')
43+
fs.mkdirSync(emptyDir)
44+
await expect(vendordep.getJsonFiles(emptyDir)).rejects.toThrow('No JSON files found in the given directory')
45+
})
3946
})
4047

41-
it('get JSON', async () => {
42-
const file = './test/vendordeps/test.json'
43-
expect(vendordep.getJson(file)).not.toThrow()
48+
describe('getJson', () => {
49+
it('should return a JSON object if the file is correctly formatted', async () => {
50+
const json = await vendordep.getJson(mockFile)
51+
expect(json).toEqual(mockJson)
52+
})
53+
54+
it('should throw an error if the JSON file is incorrectly formatted', async () => {
55+
const badJsonFile = path.join(mockDir, 'badFile.json')
56+
fs.writeFileSync(badJsonFile, JSON.stringify({}))
57+
await expect(vendordep.getJson(badJsonFile)).rejects.toThrow('JSON file is formatted incorrectly')
58+
})
4459
})
4560

46-
it('download JSON', async () => {
47-
const url = 'https://software-metadata.revrobotics.com/REVLib-2024.json'
48-
const file = './test/vendordeps/test.json'
49-
await expect(vendordep.downloadJson(url, file)).resolves.not.toThrow()
61+
describe('downloadJson', () => {
62+
it('should download and replace the JSON file', async () => {
63+
await vendordep.downloadJson(mockUrl, mockFile)
64+
const json = JSON.parse(fs.readFileSync(mockFile, 'utf8'))
65+
expect(json).toEqual(mockJson)
66+
})
5067
})
51-
})
68+
})

0 commit comments

Comments
 (0)