From 1297e8a16a2fb0ccc195c0dd14b678bb2d7fc9fc Mon Sep 17 00:00:00 2001 From: Max Romanov Date: Mon, 10 Jan 2022 16:07:31 +0300 Subject: Tests: using modules in Go. --- test/unit/applications/lang/go.py | 43 +++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 11 deletions(-) (limited to 'test/unit/applications/lang') diff --git a/test/unit/applications/lang/go.py b/test/unit/applications/lang/go.py index 367059e6..67501e9c 100644 --- a/test/unit/applications/lang/go.py +++ b/test/unit/applications/lang/go.py @@ -1,4 +1,5 @@ import os +import shutil import subprocess from unit.applications.proto import TestApplicationProto @@ -6,14 +7,25 @@ from unit.option import option class TestApplicationGo(TestApplicationProto): - def prepare_env(self, script, name, static=False): - if not os.path.exists(option.temp_dir + '/go'): - os.mkdir(option.temp_dir + '/go') + @staticmethod + def prepare_env(script, name='app', static=False): + temp_dir = option.temp_dir + '/go/' + + if not os.path.exists(temp_dir): + os.mkdir(temp_dir) + + cache_dir = option.cache_dir + '/go-build' + + if not os.path.exists(cache_dir): + os.mkdir(cache_dir) env = os.environ.copy() env['GOPATH'] = option.current_dir + '/build/go' - env['GOCACHE'] = option.cache_dir + '/go' - env['GO111MODULE'] = 'auto' + env['GOCACHE'] = cache_dir + + shutil.copy2( + option.test_dir + '/go/' + script + '/' + name + '.go', + temp_dir) if static: args = [ @@ -24,23 +36,32 @@ class TestApplicationGo(TestApplicationProto): '-ldflags', '-extldflags "-static"', '-o', - option.temp_dir + '/go/' + name, - option.test_dir + '/go/' + script + '/' + name + '.go', + temp_dir + name, + temp_dir + name + '.go', ] else: args = [ 'go', 'build', '-o', - option.temp_dir + '/go/' + name, - option.test_dir + '/go/' + script + '/' + name + '.go', + temp_dir + name, + temp_dir + name + '.go', ] + replace_path = option.current_dir + '/build/go/src/unit.nginx.org/go' + + with open(temp_dir + 'go.mod', 'w') as f: + f.write( + f"""module test/app +require unit.nginx.org/go v0.0.0 +replace unit.nginx.org/go => {replace_path} +""") + if option.detailed: print("\n$ GOPATH=" + env['GOPATH'] + " " + " ".join(args)) try: - process = subprocess.run(args, env=env) + process = subprocess.run(args, env=env, cwd=temp_dir) except KeyboardInterrupt: raise @@ -61,7 +82,7 @@ class TestApplicationGo(TestApplicationProto): executable = "/go/" + name static_build = True - self.prepare_env(script, name, static=static_build) + TestApplicationGo.prepare_env(script, name, static=static_build) conf = { "listeners": {"*:7080": {"pass": "applications/" + script}}, -- cgit From 0f725346603f4de4473d12da502104b188ac02a4 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Mon, 11 Apr 2022 21:05:14 +0100 Subject: Tests: style. --- test/unit/applications/lang/go.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'test/unit/applications/lang') diff --git a/test/unit/applications/lang/go.py b/test/unit/applications/lang/go.py index 67501e9c..04af26e1 100644 --- a/test/unit/applications/lang/go.py +++ b/test/unit/applications/lang/go.py @@ -24,8 +24,8 @@ class TestApplicationGo(TestApplicationProto): env['GOCACHE'] = cache_dir shutil.copy2( - option.test_dir + '/go/' + script + '/' + name + '.go', - temp_dir) + option.test_dir + '/go/' + script + '/' + name + '.go', temp_dir + ) if static: args = [ @@ -55,7 +55,8 @@ class TestApplicationGo(TestApplicationProto): f"""module test/app require unit.nginx.org/go v0.0.0 replace unit.nginx.org/go => {replace_path} -""") +""" + ) if option.detailed: print("\n$ GOPATH=" + env['GOPATH'] + " " + " ".join(args)) -- cgit