5
5
import * as vendordep from '../src/vendordep'
6
6
import { expect } from '@jest/globals'
7
7
import path from 'path'
8
+ import * as fs from 'fs'
8
9
9
10
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
+ }
16
25
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 ) )
22
29
} )
23
30
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 } )
28
33
} )
29
34
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
+ } )
34
40
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
+ } )
39
46
} )
40
47
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
+ } )
44
59
} )
45
60
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
+ } )
50
67
} )
51
- } )
68
+ } )
0 commit comments