Skip to content

Commit

Permalink
test subprocess.run
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Apr 2, 2024
1 parent 842101c commit 9b8a4c9
Showing 1 changed file with 260 additions and 5 deletions.
265 changes: 260 additions & 5 deletions patches/gyp-next-2.patch
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
From 4c049a0a283951886636aedbcbe629ae6443286c Mon Sep 17 00:00:00 2001
From: toyobayashi <[email protected]>
Date: Tue, 2 Apr 2024 19:26:10 +0800
Subject: [PATCH 1/5] fix: failed to detect flavor if compiler path include
Subject: [PATCH 1/8] fix: failed to detect flavor if compiler path include
white spaces

---
Expand Down Expand Up @@ -89,7 +89,7 @@ index 43b2254..da78d39 100755
From b6b68d64d8c9e3e597f1b7022872d02a11cc0240 Mon Sep 17 00:00:00 2001
From: toyobayashi <[email protected]>
Date: Tue, 2 Apr 2024 20:38:41 +0800
Subject: [PATCH 2/5] raise error if exit code is not 0
Subject: [PATCH 2/8] raise error if exit code is not 0

---
pylib/gyp/common.py | 26 ++++++++++++++++--------
Expand Down Expand Up @@ -262,7 +262,7 @@ index da78d39..a0cb2ed 100755
From 50fd4ca09eb34030adeffe5cdd53aa76294d5639 Mon Sep 17 00:00:00 2001
From: toyobayashi <[email protected]>
Date: Tue, 2 Apr 2024 20:49:04 +0800
Subject: [PATCH 3/5] fix ruff lint
Subject: [PATCH 3/8] fix ruff lint

---
pylib/gyp/common.py | 7 ++++---
Expand Down Expand Up @@ -304,7 +304,7 @@ index a0cb2ed..30678e1 100755
From 432a6dd3fc7c7f5e2cd7e1385563db49989a961c Mon Sep 17 00:00:00 2001
From: toyobayashi <[email protected]>
Date: Tue, 2 Apr 2024 21:00:54 +0800
Subject: [PATCH 4/5] pipe stderr
Subject: [PATCH 4/8] pipe stderr

---
pylib/gyp/common.py | 4 ++--
Expand Down Expand Up @@ -336,7 +336,7 @@ index 028387e..18c7d1c 100644
From 378170022093394c916d9453710b6a81df6d3f66 Mon Sep 17 00:00:00 2001
From: toyobayashi <[email protected]>
Date: Tue, 2 Apr 2024 21:02:50 +0800
Subject: [PATCH 5/5] fix test
Subject: [PATCH 5/8] fix test

---
pylib/gyp/common_test.py | 2 +-
Expand All @@ -355,3 +355,258 @@ index 30678e1..01d1987 100755
return [defines, flavor]

[defines1, _] = mock_run({}, "", [])

From 3945b0da3fbc11d1feea4913f4651febcc3fa6c3 Mon Sep 17 00:00:00 2001
From: toyobayashi <[email protected]>
Date: Tue, 2 Apr 2024 21:32:13 +0800
Subject: [PATCH 6/8] os.sep

---
pylib/gyp/common.py | 2 +-
pylib/gyp/common_test.py | 6 +++---
2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/pylib/gyp/common.py b/pylib/gyp/common.py
index 18c7d1c..faa3885 100644
--- a/pylib/gyp/common.py
+++ b/pylib/gyp/common.py
@@ -430,7 +430,7 @@ def GetCrossCompilerPredefines(): # -> dict
# setting posix=False will preserve extra '"' cause CreateProcess fail on Windows
# this makes '\' in %CC_target% and %CFLAGS% work
def replace_sep(s):
- return s.replace("\\", "/") if sys.platform == "win32" else s
+ return s.replace(os.sep, "/") if os.sep != "/" else s

if CC := os.environ.get("CC_target") or os.environ.get("CC"):
cmd += shlex.split(replace_sep(CC))
diff --git a/pylib/gyp/common_test.py b/pylib/gyp/common_test.py
index 01d1987..706babd 100755
--- a/pylib/gyp/common_test.py
+++ b/pylib/gyp/common_test.py
@@ -162,14 +162,14 @@ def mock_run(env, defines_stdout, expected_cmd):
}, defines5)
self.assertEqual("wasi", flavor5)

- original_platform = sys.platform
- sys.platform = "win32"
+ original_platform = os.sep
+ os.sep = "\\"
[defines6, flavor6] = mock_run(
{ "CC_target": "\"C:\\Program Files\\wasi-sdk\\clang.exe\"" },
"#define __wasm__ 1\n#define __wasi__ 1\n",
["C:/Program Files/wasi-sdk/clang.exe"]
)
- sys.platform = original_platform
+ os.sep = original_platform
self.assertDictEqual({ "__wasm__": "1", "__wasi__": "1" }, defines6)
self.assertEqual("wasi", flavor6)


From 86a37baf3e312d3c77db5bf2d9d2b362570fd776 Mon Sep 17 00:00:00 2001
From: toyobayashi <[email protected]>
Date: Tue, 2 Apr 2024 21:44:49 +0800
Subject: [PATCH 7/8] ruff --select=PT pylib/gyp/common_test.py

---
pylib/gyp/common_test.py | 32 +++++++++++++++-----------------
1 file changed, 15 insertions(+), 17 deletions(-)

diff --git a/pylib/gyp/common_test.py b/pylib/gyp/common_test.py
index 706babd..2b47997 100755
--- a/pylib/gyp/common_test.py
+++ b/pylib/gyp/common_test.py
@@ -25,10 +25,8 @@ def test_Valid(self):

def GetEdge(node):
return tuple(graph[node])
-
- self.assertEqual(
- gyp.common.TopologicallySorted(graph.keys(), GetEdge), ["a", "c", "d", "b"]
- )
+
+ assert gyp.common.TopologicallySorted(graph.keys(), GetEdge) == ["a", "c", "d", "b"]

def test_Cycle(self):
"""Test that an exception is thrown on a cyclic graph."""
@@ -60,7 +58,7 @@ def tearDown(self):

def assertFlavor(self, expected, argument, param):
sys.platform = argument
- self.assertEqual(expected, gyp.common.GetFlavor(param))
+ assert expected == gyp.common.GetFlavor(param)

def test_platform_default(self):
self.assertFlavor("freebsd", "freebsd9", {})
@@ -115,31 +113,31 @@ def mock_run(env, defines_stdout, expected_cmd):
return [defines, flavor]

[defines1, _] = mock_run({}, "", [])
- self.assertDictEqual({}, defines1)
+ assert {} == defines1

[defines2, flavor2] = mock_run(
{ "CC_target": "/opt/wasi-sdk/bin/clang" },
"#define __wasm__ 1\n#define __wasi__ 1\n",
["/opt/wasi-sdk/bin/clang"]
)
- self.assertDictEqual({ "__wasm__": "1", "__wasi__": "1" }, defines2)
- self.assertEqual("wasi", flavor2)
+ assert { "__wasm__": "1", "__wasi__": "1" } == defines2
+ assert "wasi" == flavor2

[defines3, flavor3] = mock_run(
{ "CC_target": "/opt/wasi-sdk/bin/clang --target=wasm32" },
"#define __wasm__ 1\n",
["/opt/wasi-sdk/bin/clang", "--target=wasm32"]
)
- self.assertDictEqual({ "__wasm__": "1" }, defines3)
- self.assertEqual("wasm", flavor3)
+ assert { "__wasm__": "1" } == defines3
+ assert "wasm" == flavor3

[defines4, flavor4] = mock_run(
{ "CC_target": "/emsdk/upstream/emscripten/emcc" },
"#define __EMSCRIPTEN__ 1\n",
["/emsdk/upstream/emscripten/emcc"]
)
- self.assertDictEqual({ "__EMSCRIPTEN__": "1" }, defines4)
- self.assertEqual("emscripten", flavor4)
+ assert { "__EMSCRIPTEN__": "1" } == defines4
+ assert "emscripten" == flavor4

# Test path which include white space
[defines5, flavor5] = mock_run(
@@ -155,12 +153,12 @@ def mock_run(env, defines_stdout, expected_cmd):
"-pthread"
]
)
- self.assertDictEqual({
+ assert {
"__wasm__": "1",
"__wasi__": "1",
"_REENTRANT": "1"
- }, defines5)
- self.assertEqual("wasi", flavor5)
+ } == defines5
+ assert "wasi" == flavor5

original_platform = os.sep
os.sep = "\\"
@@ -170,8 +168,8 @@ def mock_run(env, defines_stdout, expected_cmd):
["C:/Program Files/wasi-sdk/clang.exe"]
)
os.sep = original_platform
- self.assertDictEqual({ "__wasm__": "1", "__wasi__": "1" }, defines6)
- self.assertEqual("wasi", flavor6)
+ assert { "__wasm__": "1", "__wasi__": "1" } == defines6
+ assert "wasi" == flavor6

if __name__ == "__main__":
unittest.main()

From 368945156ba4d73b533ff5344779deded125bed1 Mon Sep 17 00:00:00 2001
From: toyobayashi <[email protected]>
Date: Tue, 2 Apr 2024 21:56:41 +0800
Subject: [PATCH 8/8] use subprocess.run

---
pylib/gyp/common.py | 22 ++++++++--------------
pylib/gyp/common_test.py | 20 ++++++++------------
2 files changed, 16 insertions(+), 26 deletions(-)

diff --git a/pylib/gyp/common.py b/pylib/gyp/common.py
index faa3885..5a59e9f 100644
--- a/pylib/gyp/common.py
+++ b/pylib/gyp/common.py
@@ -448,26 +448,20 @@ def replace_sep(s):
real_cmd = [*cmd, "-dM", "-E", "-x", "c", input]
try:
os.close(fd)
- out = subprocess.Popen(
- real_cmd,
- shell=True,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE
- )
- stdout, stderr = out.communicate()
+ stdout = subprocess.run(
+ real_cmd, shell=True,
+ capture_output=True, check=True
+ ).stdout
finally:
os.unlink(input)
else:
input = "/dev/null"
real_cmd = [*cmd, "-dM", "-E", "-x", "c", input]
- out = subprocess.Popen(
- real_cmd,
- shell=False,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE
- )
- stdout, stderr = out.communicate()
+ stdout = subprocess.run(
+ real_cmd, shell=False,
+ capture_output=True, check=True
+ ).stdout

- if out.returncode != 0:
- raise subprocess.CalledProcessError(out.returncode, real_cmd, stdout, stderr)
defines = {}
lines = stdout.decode("utf-8").replace("\r\n", "\n").split("\n")
for line in lines:
diff --git a/pylib/gyp/common_test.py b/pylib/gyp/common_test.py
index 2b47997..c4d4e21 100755
--- a/pylib/gyp/common_test.py
+++ b/pylib/gyp/common_test.py
@@ -10,7 +10,6 @@
import unittest
import sys
import os
-import subprocess
from unittest.mock import patch, MagicMock

class TestTopologicallySorted(unittest.TestCase):
@@ -25,8 +24,9 @@ def test_Valid(self):

def GetEdge(node):
return tuple(graph[node])
-
- assert gyp.common.TopologicallySorted(graph.keys(), GetEdge) == ["a", "c", "d", "b"]
+
+ assert gyp.common.TopologicallySorted(
+ graph.keys(), GetEdge) == ["a", "c", "d", "b"]

def test_Cycle(self):
"""Test that an exception is thrown on a cyclic graph."""
@@ -89,27 +89,23 @@ def test_GetCrossCompilerPredefines(self, mock_mkstemp, mock_unlink, mock_close)
mock_mkstemp.return_value = (0, "temp.c")

def mock_run(env, defines_stdout, expected_cmd):
- with patch("subprocess.Popen") as mock_popen:
+ with patch("subprocess.run") as mock_run:
mock_process = MagicMock()
- mock_process.communicate.return_value = (
- TestGetFlavor.MockCommunicate(defines_stdout),
- TestGetFlavor.MockCommunicate("")
- )
mock_process.returncode = 0
- mock_process.stdout = MagicMock()
- mock_popen.return_value = mock_process
+ mock_process.stdout = TestGetFlavor.MockCommunicate(defines_stdout)
+ mock_run.return_value = mock_process
expected_input = "temp.c" if sys.platform == "win32" else "/dev/null"
with patch.dict(os.environ, env):
defines = gyp.common.GetCrossCompilerPredefines()
flavor = gyp.common.GetFlavor({})
if env.get("CC_target"):
- mock_popen.assert_called_with(
+ mock_run.assert_called_with(
[
*expected_cmd,
"-dM", "-E", "-x", "c", expected_input
],
shell=sys.platform == "win32",
- stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ capture_output=True, check=True)
return [defines, flavor]

[defines1, _] = mock_run({}, "", [])

0 comments on commit 9b8a4c9

Please sign in to comment.