11import { join } from "path"
22import { writeJson } from "fs-extra"
3- import { afterEach , beforeEach , expect , suite , test } from "vitest"
3+ import { afterEach , beforeEach , expect , suite , test , vi } from "vitest"
44import type { BuildCommandOptions , BuildConfiguration , BuildConfigurations , Options } from "../src/config-types.d.js"
5- import { getBuildConfig , getConfigFile , parseBuildConfigs , parseBuiltInConfigs } from "../src/config.js"
5+ import {
6+ detectCrossCompilation ,
7+ getBuildConfig ,
8+ getConfigFile ,
9+ parseBuildConfigs ,
10+ parseBuiltInConfigs ,
11+ } from "../src/config.js"
12+ import { logger } from "../src/lib.js"
613
714suite ( "Config Functions" , ( ) => {
15+ logger . setLevel ( "debug" )
16+
817 const mockBuildOptions : BuildCommandOptions = {
918 configs : [ ] ,
1019 addonSubdirectory : "" ,
@@ -35,8 +44,10 @@ suite("Config Functions", () => {
3544
3645 beforeEach ( async ( ) => {
3746 // Reset environment variables
38- process . env . npm_config_target_arch = undefined
39- process . env . npm_config_target_os = undefined
47+ // biome-ignore lint/performance/noDelete: https://github.com/biomejs/biome/issues/5643
48+ delete process . env . npm_config_target_arch
49+ // biome-ignore lint/performance/noDelete: https://github.com/biomejs/biome/issues/5643
50+ delete process . env . npm_config_target_os
4051
4152 // Create test package.json
4253 await writeJson ( testPackageJsonPath , {
@@ -48,8 +59,10 @@ suite("Config Functions", () => {
4859
4960 afterEach ( async ( ) => {
5061 // Clean up environment variables
51- process . env . npm_config_target_arch = undefined
52- process . env . npm_config_target_os = undefined
62+ // biome-ignore lint/performance/noDelete: https://github.com/biomejs/biome/issues/5643
63+ delete process . env . npm_config_target_arch
64+ // biome-ignore lint/performance/noDelete: https://github.com/biomejs/biome/issues/5643
65+ delete process . env . npm_config_target_os
5366
5467 // Remove test package.json
5568 try {
@@ -122,7 +135,7 @@ suite("Config Functions", () => {
122135
123136 test ( "should handle cross compilation flags" , async ( ) => {
124137 const partialConfig : Partial < BuildConfiguration > = {
125- os : "win32" ,
138+ os : process . platform === "win32" ? "linux" : "win32" ,
126139 arch : "x64" ,
127140 }
128141
@@ -131,7 +144,7 @@ suite("Config Functions", () => {
131144 expect ( result . cross ) . toBe ( true )
132145 } )
133146
134- test ( "should set cross flag when npm_config_target_arch differs from process .arch" , async ( ) => {
147+ test ( "should respect npm_config_target_arch when it matches config .arch" , async ( ) => {
135148 // Set npm_config_target_arch to a different architecture than the current one
136149 process . env . npm_config_target_arch = process . arch === "x64" ? "arm64" : "x64"
137150
@@ -146,19 +159,55 @@ suite("Config Functions", () => {
146159 expect ( result . arch ) . toBe ( process . env . npm_config_target_arch )
147160 } )
148161
149- test ( "should set cross flag when npm_config_target_arch differs from process.arch when no config file is provided" , async ( ) => {
162+ test ( "should respect config.arch when it matches process.arch" , async ( ) => {
163+ // Mock process.arch
164+ vi . spyOn ( process , "arch" , "get" ) . mockReturnValue ( "arm64" )
165+
166+ const partialConfig : Partial < BuildConfiguration > = {
167+ os : process . platform ,
168+ arch : process . arch ,
169+ }
170+
171+ console . log ( process . arch , detectCrossCompilation ( mockConfigFile , partialConfig ) )
172+
173+ const result = await getBuildConfig ( mockBuildOptions , partialConfig , mockConfigFile )
174+
175+ expect ( result . cross ) . toBe ( false )
176+ expect ( result . arch ) . toBe ( process . arch )
177+
178+ vi . restoreAllMocks ( )
179+ } )
180+
181+ test ( "should respect config.arch when it differs from process.arch" , async ( ) => {
182+ // Mock process.arch
183+ vi . spyOn ( process , "arch" , "get" ) . mockReturnValue ( "arm64" )
184+
185+ const partialConfig : Partial < BuildConfiguration > = {
186+ os : process . platform ,
187+ arch : "x64" , // Different from process.arch
188+ }
189+
190+ const result = await getBuildConfig ( mockBuildOptions , partialConfig , mockConfigFile )
191+
192+ expect ( result . cross ) . toBe ( true )
193+ expect ( result . arch ) . toBe ( "x64" )
194+
195+ vi . restoreAllMocks ( )
196+ } )
197+
198+ test ( "should respect npm_config_target_arch when it differs from process.arch" , async ( ) => {
150199 // Set npm_config_target_arch to a different architecture than the current one
151200 process . env . npm_config_target_arch = process . arch === "x64" ? "arm64" : "x64"
152201
153202 const partialConfig : Partial < BuildConfiguration > = {
154203 os : process . platform ,
155- arch : process . env . npm_config_target_arch as NodeJS . Architecture ,
204+ arch : process . arch ,
156205 }
157206
158- const result = await getBuildConfig ( mockBuildOptions , partialConfig , { } )
207+ const result = await getBuildConfig ( mockBuildOptions , partialConfig , mockConfigFile )
159208
160209 expect ( result . cross ) . toBe ( true )
161- expect ( result . arch ) . toBe ( process . env . npm_config_target_arch )
210+ expect ( result . arch ) . toBe ( process . arch )
162211 } )
163212
164213 test ( "should use default values when no config file is provided" , async ( ) => {
0 commit comments