9
9
createAnyInstanceSerializer ,
10
10
createAbsolutePathSerializer ,
11
11
} from '@osd/dev-utils' ;
12
- import { Build , Config } from '../lib' ;
13
- import { PatchNativeModules } from './patch_native_modules_task' ;
12
+ import { Build , Config , read , download , untar , gunzip } from '../lib' ;
13
+ import { createPatchNativeModulesTask } from './patch_native_modules_task' ;
14
14
15
15
const log = new ToolingLog ( ) ;
16
16
const testWriter = new ToolingLogCollectingWriter ( ) ;
@@ -19,16 +19,16 @@ expect.addSnapshotSerializer(createAnyInstanceSerializer(Config));
19
19
expect . addSnapshotSerializer ( createAnyInstanceSerializer ( ToolingLog ) ) ;
20
20
expect . addSnapshotSerializer ( createAbsolutePathSerializer ( ) ) ;
21
21
22
- jest . mock ( '../lib/download' ) ;
23
- jest . mock ( '../lib/fs' , ( ) => ( {
24
- ... jest . requireActual ( '../lib/fs' ) ,
25
- untar : jest . fn ( ) ,
26
- gunzip : jest . fn ( ) ,
27
- } ) ) ;
28
-
29
- const { untar } = jest . requireMock ( '../lib/fs' ) ;
30
- const { gunzip } = jest . requireMock ( '../lib/fs' ) ;
31
- const { download } = jest . requireMock ( '../lib/download' ) ;
22
+ jest . mock ( '../lib' , ( ) => {
23
+ const originalModule = jest . requireActual ( '../lib' ) ;
24
+ return {
25
+ ... originalModule ,
26
+ download : jest . fn ( ) ,
27
+ gunzip : jest . fn ( ) ,
28
+ untar : jest . fn ( ) ,
29
+ read : jest . fn ( ) ,
30
+ } ;
31
+ } ) ;
32
32
33
33
async function setup ( ) {
34
34
const config = await Config . create ( {
@@ -38,14 +38,15 @@ async function setup() {
38
38
linux : false ,
39
39
linuxArm : false ,
40
40
darwin : false ,
41
+ windows : false ,
41
42
} ,
42
43
} ) ;
43
44
44
45
const build = new Build ( config ) ;
45
46
46
- download . mockImplementation ( ( ) => { } ) ;
47
- untar . mockImplementation ( ( ) => { } ) ;
48
- gunzip . mockImplementation ( ( ) => { } ) ;
47
+ ( read as jest . MockedFunction < typeof read > ) . mockImplementation ( async ( ) => {
48
+ return JSON . stringify ( { version : mockPackage . version } ) ;
49
+ } ) ;
49
50
50
51
return { config, build } ;
51
52
}
@@ -55,38 +56,77 @@ beforeEach(() => {
55
56
jest . clearAllMocks ( ) ;
56
57
} ) ;
57
58
58
- it ( 'patch native modules task downloads the correct platform package' , async ( ) => {
59
- const { config, build } = await setup ( ) ;
60
- config . targetPlatforms . linuxArm = true ;
61
- await PatchNativeModules . run ( config , log , build ) ;
62
- expect ( download . mock . calls . length ) . toBe ( 1 ) ;
63
- expect ( download . mock . calls ) . toMatchInlineSnapshot ( `
59
+ const mockPackage = {
60
+ name : 'mock-native-module' ,
61
+ version : '1.0.0' ,
62
+ destinationPath : 'path/to/destination' ,
63
+ extractMethod : 'untar' ,
64
+ archives : {
65
+ 'linux-arm64' : {
66
+ url : 'https://example.com/mock-native-module/linux-arm64.tar.gz' ,
67
+ sha256 : 'mock-sha256' ,
68
+ } ,
69
+ 'linux-x64' : {
70
+ url : 'https://example.com/mock-native-module/linux-x64.gz' ,
71
+ sha256 : 'mock-sha256' ,
72
+ } ,
73
+ } ,
74
+ } ;
75
+
76
+ describe ( 'patch native modules task' , ( ) => {
77
+ it ( 'patch native modules task downloads the correct platform package' , async ( ) => {
78
+ const { config, build } = await setup ( ) ;
79
+ config . targetPlatforms . linuxArm = true ;
80
+ const PatchNativeModulesWithMock = createPatchNativeModulesTask ( [ mockPackage ] ) ;
81
+ await PatchNativeModulesWithMock . run ( config , log , build ) ;
82
+ expect ( ( download as jest . MockedFunction < typeof download > ) . mock . calls . length ) . toBe ( 1 ) ;
83
+ expect ( ( download as jest . MockedFunction < typeof download > ) . mock . calls ) . toMatchInlineSnapshot ( `
64
84
Array [
65
85
Array [
66
86
Object {
67
- "destination": <absolute path>/.native_modules/re2 /linux-arm64-83 .tar.gz,
87
+ "destination": <absolute path>/.native_modules/mock-native-module /linux-arm64.tar.gz,
68
88
"log": <ToolingLog>,
69
89
"retries": 3,
70
- "sha256": "d86ced75b794fbf518b90908847b3c09a50f3ff5a2815aa30f53080f926a2873 ",
71
- "url": "https://d1v1sj258etie.cloudfront.net/node-re2/releases/download/1.17.4/ linux-arm64-83 .tar.gz",
90
+ "sha256": "mock-sha256 ",
91
+ "url": "https://example.com/mock-native-module/ linux-arm64.tar.gz",
72
92
},
73
93
],
74
94
]
75
95
` ) ;
76
- } ) ;
96
+ } ) ;
77
97
78
- it ( 'for .tar.gz artifact, patch native modules task unzip it via untar' , async ( ) => {
79
- const { config, build } = await setup ( ) ;
80
- config . targetPlatforms . linuxArm = true ;
81
- await PatchNativeModules . run ( config , log , build ) ;
82
- expect ( untar . mock . calls . length ) . toBe ( 1 ) ;
83
- expect ( gunzip . mock . calls . length ) . toBe ( 0 ) ;
84
- } ) ;
98
+ it ( 'for .tar.gz artifact, patch native modules task unzip it via untar' , async ( ) => {
99
+ const { config, build } = await setup ( ) ;
100
+ config . targetPlatforms . linuxArm = true ;
101
+ const PatchNativeModulesWithMock = createPatchNativeModulesTask ( [ mockPackage ] ) ;
102
+ await PatchNativeModulesWithMock . run ( config , log , build ) ;
103
+ expect ( untar ) . toHaveBeenCalled ( ) ;
104
+ expect ( gunzip ) . not . toHaveBeenCalled ( ) ;
105
+ } ) ;
85
106
86
- it ( 'for .gz artifact, patch native modules task unzip it via gunzip' , async ( ) => {
87
- const { config, build } = await setup ( ) ;
88
- config . targetPlatforms . linux = true ;
89
- await PatchNativeModules . run ( config , log , build ) ;
90
- expect ( untar . mock . calls . length ) . toBe ( 0 ) ;
91
- expect ( gunzip . mock . calls . length ) . toBe ( 1 ) ;
107
+ it ( 'for .gz artifact, patch native modules task unzip it via gunzip' , async ( ) => {
108
+ const mockPackageGZ = {
109
+ ...mockPackage ,
110
+ extractMethod : 'gunzip' ,
111
+ } ;
112
+ const { config, build } = await setup ( ) ;
113
+ config . targetPlatforms . linux = true ;
114
+ const PatchNativeModulesWithMock = createPatchNativeModulesTask ( [ mockPackageGZ ] ) ;
115
+ await PatchNativeModulesWithMock . run ( config , log , build ) ;
116
+ expect ( gunzip ) . toHaveBeenCalled ( ) ;
117
+ expect ( untar ) . not . toHaveBeenCalled ( ) ;
118
+ } ) ;
119
+
120
+ it ( 'throws error for unsupported extract methods' , async ( ) => {
121
+ const mockPackageUnsupported = {
122
+ ...mockPackage ,
123
+ extractMethod : 'unsupported' ,
124
+ } ;
125
+ const { config, build } = await setup ( ) ;
126
+ config . targetPlatforms . linux = true ;
127
+ const PatchNativeModulesWithMock = createPatchNativeModulesTask ( [ mockPackageUnsupported ] ) ;
128
+ await expect ( PatchNativeModulesWithMock . run ( config , log , build ) ) . rejects . toThrow (
129
+ 'Extract method of unsupported is not supported'
130
+ ) ;
131
+ } ) ;
92
132
} ) ;
0 commit comments