Commit 9fcac418 authored by Mathieu Giraud's avatar Mathieu Giraud

src/should.py: refactor, extract .launch() and .cmd_variables_cd from TestSuite.test()

Prepares #36
parent 7595dcbc
...@@ -697,6 +697,14 @@ class TestSuite(): ...@@ -697,6 +697,14 @@ class TestSuite():
self.elapsed_time = None self.elapsed_time = None
self.timeout = timeout self.timeout = timeout
def cmd_variables_cd(self, cmd, verbose, colorize):
cmd = replace_variables(cmd, self.variables_all)
if self.cd:
cmd = 'cd %s ; ' % self.cd + cmd
if verbose > 0:
print(color(ANSI.MAGENTA, cmd, colorize))
return cmd
def load(self, should_lines): def load(self, should_lines):
name = '' name = ''
this_cmd_continues = False this_cmd_continues = False
...@@ -784,18 +792,11 @@ class TestSuite(): ...@@ -784,18 +792,11 @@ class TestSuite():
if verbose > 1: if verbose > 1:
print_variables(self.variables_all) print_variables(self.variables_all)
def cmd_variables_cd(cmd):
cmd = replace_variables(cmd, self.variables_all)
if self.cd:
cmd = 'cd %s ; ' % self.cd + cmd
if verbose > 0:
print(color(ANSI.MAGENTA, cmd, colorize))
return cmd
self.status = None self.status = None
if self.requires_cmd: if self.requires_cmd:
requires_cmd = cmd_variables_cd(self.requires_cmd) requires_cmd = self.cmd_variables_cd(self.requires_cmd, verbose, colorize)
p = subprocess.Popen(requires_cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) p = subprocess.Popen(requires_cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
self.requires = (p.wait() == 0) self.requires = (p.wait() == 0)
self.requires_stderr = [l.decode(errors='replace') for l in p.stderr.readlines()] self.requires_stderr = [l.decode(errors='replace') for l in p.stderr.readlines()]
...@@ -811,10 +812,18 @@ class TestSuite(): ...@@ -811,10 +812,18 @@ class TestSuite():
self.skip_all('%s while %s is given' % (DIRECTIVE_NO_LAUNCHER, VAR_LAUNCHER), verbose) self.skip_all('%s while %s is given' % (DIRECTIVE_NO_LAUNCHER, VAR_LAUNCHER), verbose)
return self.status return self.status
self.test_lines += self.launch(self.cmds, verbose, colorize)
self.tests_on_lines(self.tests, self.test_lines, verbose, colorize)
self.debug(self.status, "\n".join(self.cmds), self.test_lines, verbose, colorize)
return self.status
def launch(self, cmds, verbose, colorize):
start_time = time.time() start_time = time.time()
cmd = ' ; '.join(map(pre_process, self.cmds)) cmd = ' ; '.join(map(pre_process, cmds))
cmd = cmd_variables_cd(cmd) cmd = self.cmd_variables_cd(cmd, verbose, colorize)
f_stdout = tempfile.TemporaryFile() f_stdout = tempfile.TemporaryFile()
f_stderr = tempfile.TemporaryFile() f_stderr = tempfile.TemporaryFile()
...@@ -830,6 +839,10 @@ class TestSuite(): ...@@ -830,6 +839,10 @@ class TestSuite():
self.tests.append(ExternalTestCase('Exit code is %d' % self.expected_exit_code, SKIP, 'timeout after %s seconds' % self.timeout)) self.tests.append(ExternalTestCase('Exit code is %d' % self.expected_exit_code, SKIP, 'timeout after %s seconds' % self.timeout))
p.kill() p.kill()
if self.elapsed_time is None:
self.elapsed_time = 0
self.elapsed_time += time.time() - start_time
f_stdout.seek(0) f_stdout.seek(0)
f_stderr.seek(0) f_stderr.seek(0)
self.stdout = [l.decode(errors='replace') for l in f_stdout.readlines()] self.stdout = [l.decode(errors='replace') for l in f_stdout.readlines()]
...@@ -837,18 +850,12 @@ class TestSuite(): ...@@ -837,18 +850,12 @@ class TestSuite():
f_stdout.close() f_stdout.close()
f_stderr.close() f_stderr.close()
if verbose > 0: if verbose > 0:
self.print_stderr(colorize) self.print_stderr(colorize)
self.test_lines = open(self.source).readlines() if self.source else self.stdout return open(self.source).readlines() if self.source else self.stdout
self.tests_on_lines(self.tests, self.test_lines, verbose, colorize)
self.debug(self.status, cmd, self.test_lines, verbose, colorize)
self.elapsed_time = time.time() - start_time
return self.status
def tests_on_lines(self, tests, test_lines, verbose, colorize): def tests_on_lines(self, tests, test_lines, verbose, colorize):
''' '''
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment