77"""Unit tests for the common.py file."""
88
99import os
10+ import subprocess
1011import sys
1112import unittest
1213from unittest .mock import MagicMock , patch
@@ -85,22 +86,34 @@ def decode(self, encoding):
8586 @patch ("os.close" )
8687 @patch ("os.unlink" )
8788 @patch ("tempfile.mkstemp" )
88- def test_GetCrossCompilerPredefines (self , mock_mkstemp , mock_unlink , mock_close ):
89+ def test_GetCompilerPredefines (self , mock_mkstemp , mock_unlink , mock_close ):
8990 mock_close .return_value = None
9091 mock_unlink .return_value = None
9192 mock_mkstemp .return_value = (0 , "temp.c" )
9293
93- def mock_run (env , defines_stdout , expected_cmd ):
94+ def mock_run (env , defines_stdout , expected_cmd , throws = False ):
9495 with patch ("subprocess.run" ) as mock_run :
95- mock_process = MagicMock ()
96- mock_process .returncode = 0
97- mock_process .stdout = TestGetFlavor .MockCommunicate (defines_stdout )
98- mock_run .return_value = mock_process
9996 expected_input = "temp.c" if sys .platform == "win32" else "/dev/null"
97+ if throws :
98+ mock_run .side_effect = subprocess .CalledProcessError (
99+ returncode = 1 ,
100+ cmd = [
101+ * expected_cmd ,
102+ "-dM" , "-E" , "-x" , "c" , expected_input
103+ ]
104+ )
105+ else :
106+ mock_process = MagicMock ()
107+ mock_process .returncode = 0
108+ mock_process .stdout = TestGetFlavor .MockCommunicate (defines_stdout )
109+ mock_run .return_value = mock_process
100110 with patch .dict (os .environ , env ):
101- defines = gyp .common .GetCrossCompilerPredefines ()
111+ try :
112+ defines = gyp .common .GetCompilerPredefines ()
113+ except Exception as e :
114+ self .fail (f"GetCompilerPredefines raised an exception: { e } " )
102115 flavor = gyp .common .GetFlavor ({})
103- if env .get ("CC_target" ):
116+ if env .get ("CC_target" ) or env . get ( "CC" ) :
104117 mock_run .assert_called_with (
105118 [
106119 * expected_cmd ,
@@ -110,6 +123,9 @@ def mock_run(env, defines_stdout, expected_cmd):
110123 capture_output = True , check = True )
111124 return [defines , flavor ]
112125
126+ [defines0 , _ ] = mock_run ({ "CC" : "cl.exe" }, "" , ["cl.exe" ], True )
127+ assert defines0 == {}
128+
113129 [defines1 , _ ] = mock_run ({}, "" , [])
114130 assert defines1 == {}
115131
0 commit comments