Tests: using modules in Go.
This commit is contained in:
@@ -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}},
|
||||
|
||||
@@ -1,34 +1,8 @@
|
||||
import os
|
||||
import subprocess
|
||||
from unit.applications.lang.go import TestApplicationGo
|
||||
|
||||
|
||||
def check_go(current_dir, temp_dir, test_dir):
|
||||
if not os.path.exists(temp_dir + '/go'):
|
||||
os.mkdir(temp_dir + '/go')
|
||||
def check_go():
|
||||
process = TestApplicationGo.prepare_env('empty')
|
||||
|
||||
env = os.environ.copy()
|
||||
env['GOPATH'] = current_dir + '/build/go'
|
||||
env['GO111MODULE'] = 'auto'
|
||||
|
||||
try:
|
||||
process = subprocess.run(
|
||||
[
|
||||
'go',
|
||||
'build',
|
||||
'-o',
|
||||
temp_dir + '/go/app',
|
||||
test_dir + '/go/empty/app.go',
|
||||
],
|
||||
env=env,
|
||||
stderr=subprocess.STDOUT,
|
||||
stdout=subprocess.PIPE,
|
||||
)
|
||||
|
||||
if process.returncode == 0:
|
||||
return True
|
||||
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
|
||||
except subprocess.CalledProcessError:
|
||||
return None
|
||||
if process != None and process.returncode == 0:
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user