def testEnvironmentCommand(self): dir = VirtualDirectory() bar = Artifact("env/BAR", None) bar_set = Artifact("env/set/BAR", None) output = Artifact("foo", None) command = EnvironmentCommand(MockRuleContext(bar, bar_set), "BAR", output) enumerator = MockArtifactEnumerator({bar_set: "false"}) command.enumerate_artifacts(enumerator) self.assertEquals([bar_set], enumerator.reads) self.assertEquals([], enumerator.inputs) self.assertEquals([output], enumerator.outputs) enumerator = MockArtifactEnumerator({bar_set: "true"}) command.enumerate_artifacts(enumerator) self.assertEquals([bar_set], enumerator.reads) self.assertEquals([bar], enumerator.inputs) self.assertEquals([output], enumerator.outputs) context = MockCommandContext(dir) dir.write("env/set/BAR", "false") log = cStringIO.StringIO() self.assertFalse(command.run(context, log)) self.assertFalse(dir.exists("foo")) self.assertEquals("Environment variable not set: BAR\n", log.getvalue()) dir.write("env/set/BAR", "true") dir.write("env/BAR", "baz") log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertEquals("baz", dir.read("foo")) self.assertEquals("", log.getvalue()) self.assertEquals("echo $BAR > foo\n", _print_command(command))
def testEnvironmentCommandWithDefault(self): bar = Artifact("env/BAR", None) bar_set = Artifact("env/set/BAR", None) default = Artifact("default", None) output = Artifact("foo", None) command = EnvironmentCommand(MockRuleContext(bar, bar_set), "BAR", output, default) enumerator = MockArtifactEnumerator({bar_set: "false"}) command.enumerate_artifacts(enumerator) self.assertEquals([bar_set], enumerator.reads) self.assertEquals([default], enumerator.inputs) self.assertEquals([output], enumerator.outputs) enumerator = MockArtifactEnumerator({bar_set: "true"}) command.enumerate_artifacts(enumerator) self.assertEquals([bar_set], enumerator.reads) self.assertEquals([bar], enumerator.inputs) self.assertEquals([output], enumerator.outputs) dir = VirtualDirectory() dir.write("env/set/BAR", "true") dir.write("env/BAR", "baz") context = MockCommandContext(dir) log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertEquals("baz", dir.read("foo")) self.assertEquals("", log.getvalue()) dir = VirtualDirectory() dir.write("env/set/BAR", "false") dir.write("default", "qux") context = MockCommandContext(dir) log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertEquals("qux", dir.read("foo")) self.assertEquals("", log.getvalue()) self.assertEquals("echo ${BAR:$(default)} > foo\n", _print_command(command))
def testEchoCommand(self): dir = VirtualDirectory() output = Artifact("foo", None) command = EchoCommand("bar", output) enumerator = MockArtifactEnumerator() command.enumerate_artifacts(enumerator) self.assertEquals([], enumerator.reads) self.assertEquals([], enumerator.inputs) self.assertEquals([output], enumerator.outputs) context = MockCommandContext(dir) log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertEquals("bar", dir.read("foo")) self.assertEquals("", log.getvalue()) self.assertEquals("echo 'bar' > foo\n", _print_command(command))
class Configuration(object): def __init__(self, output_path, all_configs=None): # We want to make sure to construct only one copy of each config, even # if configs refer to each other or multiple configs refer to a shared # config. So, all_configs maps names to configs that we have already # constructed. if all_configs is None: # Note that if we just make all_configs default to {} in the method # signature, then Python will create a single empty map to use as the # default value for all calls rather than create a new one every call. # Since we modify all_configs during this method, we would be modifying # the shared default value, which would be bad. If you don't understand # what I mean, try typing the following into the interpreter and then # calling it several times with no argument: # def f(l = []): # l.append("foo") # return l # Ouchies. all_configs = {} if output_path is None: all_configs[""] = self else: all_configs[output_path] = self self.name = output_path self.source_dir = DiskDirectory(".") if output_path is None: self.output_dir = self.source_dir else: self.source_dir.mkdir(output_path) self.output_dir = DiskDirectory(output_path) self.mem_dir = VirtualDirectory() self.env_dir = VirtualDirectory() _restore_pickle(self.mem_dir, self.output_dir, "mem.pickle") _restore_pickle(self.env_dir, self.output_dir, "env.pickle") self.alt_configs = {} self.__make_root_dir() self.alt_configs["host"] = self if self.env_dir.exists("$mappings"): mappings = self.env_dir.read("$mappings").split(":") for mapping in mappings: if mapping == "": continue alias, name = mapping.split("=", 1) if name in all_configs: self.alt_configs[alias] = all_configs[name] else: if name == "": name = None self.alt_configs[alias] = Configuration(name, all_configs) def __make_root_dir(self): self.mapping = _WorkingDirMapping(self.source_dir, self.output_dir, self.mem_dir, self.env_dir, self.alt_configs) self.root_dir = MappedDirectory(self.mapping) def save(self): if not self.mem_dir.empty(): _save_pickle(self.mem_dir, self.root_dir, "mem.pickle") if not self.env_dir.empty(): _save_pickle(self.env_dir, self.root_dir, "env.pickle") def getenv(self, name): if self.root_dir.read("env/set/" + name) == "true": return self.root_dir.read("env/" + name) else: return None def clean(self, expunge=False): for dir in ["tmp", "bin", "lib", "share", "include", "mem", "env"]: if self.root_dir.exists(dir): shutil.rmtree(self.root_dir.get_disk_path(dir)) for file in ["mem.pickle", "env.pickle"]: if self.root_dir.exists(file): os.remove(self.root_dir.get_disk_path(file)) self.mem_dir = VirtualDirectory() if expunge: # Try to remove the output directory itself -- will fail if not empty. outdir = self.root_dir.get_disk_path(".") if outdir.endswith("/."): # rmdir doesn't like a trailing "/.". outdir = outdir[:-2] try: os.rmdir(outdir) except os.error: pass else: # Restore the parts of env.pickle that were set explicitly. new_env_dir = VirtualDirectory() if self.env_dir.exists("$mappings"): new_env_dir.write("$mappings", self.env_dir.read("$mappings")) if self.env_dir.exists("$config"): locked_vars = self.env_dir.read("$config") new_env_dir.write("$config", locked_vars) for var in locked_vars.split(","): if var != "": new_env_dir.write(var, self.env_dir.read(var)) new_env_dir.write("set/" + var, self.env_dir.read("set/" + var)) self.env_dir = new_env_dir self.__make_root_dir() def get_all_linked_configs(self): result = set() self.__get_all_linked_configs_recursive(result) return result def __get_all_linked_configs_recursive(self, result): if self in result: return result.add(self) for link in self.alt_configs.values(): link.__get_all_linked_configs_recursive(result)
class SubprocessCommandTest(unittest.TestCase): def setUp(self): self.__action = Action(None, "dummy", "dummy") self.__artifact = Artifact("filename", None) self.__dir = VirtualDirectory() def testVerifyArgs(self): action = self.__action artifact = self.__artifact self.assertRaises(TypeError, SubprocessCommand, action, [0]) self.assertRaises(TypeError, SubprocessCommand, action, [[0]]) self.assertRaises(TypeError, SubprocessCommand, action, [artifact, 0]) SubprocessCommand(action, ["foo"]) SubprocessCommand(action, [artifact]) SubprocessCommand(action, [artifact]) SubprocessCommand(action, [[artifact]]) SubprocessCommand(action, [[artifact]]) SubprocessCommand(action, [artifact.contents()]) def testEnumerateArtifacts(self): action = self.__action inputs = [ Artifact("input1", None), Artifact("input2", None) ] outputs = [ Artifact("output1", action), Artifact("output2", action) ] command = SubprocessCommand(action, ["foo", inputs[0], outputs[0]], implicit = [inputs[1], outputs[1]]) enumerator = MockArtifactEnumerator() command.enumerate_artifacts(enumerator) self.assertEquals([], enumerator.reads) self.assertEquals(set(inputs), set(enumerator.inputs)) self.assertEquals(set(outputs), set(enumerator.outputs)) stdout = Artifact("stdout", None) stderr = Artifact("stderr", None) exit_code = Artifact("error_code", None) command = SubprocessCommand(action, inputs + outputs, capture_stdout = stdout, capture_stderr = stderr, capture_exit_status = exit_code) enumerator = MockArtifactEnumerator() command.enumerate_artifacts(enumerator) self.assertEquals([], enumerator.reads) self.assertEquals(set(inputs), set(enumerator.inputs)) self.assertEquals(set(outputs + [stdout, stderr, exit_code]), set(enumerator.outputs)) def testFormatArgs(self): artifact = self.__artifact self.__dir.write(self.__artifact.filename, "content") self.assertFormattedAs(["foo"], ["foo"]) self.assertFormattedAs([artifact], ["disk/filename"], "filename") self.assertFormattedAs([artifact.contents()], ["content"], "$(filename)") self.assertFormattedAs([["foo"]], ["foo"]) self.assertFormattedAs([[artifact]], ["disk/filename"], "filename") self.assertFormattedAs([[artifact.contents()]], ["content"], "$(filename)") self.assertFormattedAs(["foo", ["bar", ["baz", "qux"], "corge"], "grault"], ["foo", "barbazquxcorge", "grault"]) self.assertFormattedAs(["(", artifact, ")"], ["(", "disk/filename", ")"], "( filename )") self.assertFormattedAs([["(", artifact, ")"]], ["(disk/filename)"], "(filename)") self.__dir.write(self.__artifact.filename, "content with\nspaces") self.assertFormattedAs(["(", artifact.contents(), ")"], ["(", "content", "with", "spaces", ")"], "( $(filename) )") self.assertFormattedAs([["(", artifact.contents(), ")"]], ["(content with\nspaces)"], "($(filename))") self.assertFormattedAs(["(", SubprocessCommand.Quoted(["foo bar", "baz"]), ")"], ["(", "'foo bar' baz", ")"]) self.assertFormattedAs([SubprocessCommand.Quoted(["'hello'"])], ["\"'hello'\""]) self.assertFormattedAs([SubprocessCommand.Quoted(["(", artifact, ")"])], ["'(' disk/filename ')'"], "'(' filename ')'") def assertFormattedAs(self, args, result, printed = None): context = MockCommandContext(self.__dir, diskpath_prefix = "disk/") command = SubprocessCommand(self.__action, list(args)) self.assertTrue(command.run(context, cStringIO.StringIO())) self.assertEquals(result, context.subprocess_args) if printed is None: printed = " ".join(result) self.assertEquals(printed + "\n", _print_command(command)) def testRedirectStreams(self): # No redirection. command = SubprocessCommand(self.__action, ["foo"]) context = MockCommandContext(self.__dir) context.subprocess_result = (0, "some text", None) log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertTrue(context.subprocess_kwargs["stdout"] is subprocess.PIPE) self.assertTrue(context.subprocess_kwargs["stderr"] is subprocess.STDOUT) self.assertEquals("some text", log.getvalue()) self.assertEquals("foo\n", _print_command(command)) # Redirect stdout. command = SubprocessCommand(self.__action, ["foo"], capture_stdout = self.__artifact) context = MockCommandContext(self.__dir) context.subprocess_result = (0, "some text", "error text") log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertTrue(context.subprocess_kwargs["stdout"] is subprocess.PIPE) self.assertTrue(context.subprocess_kwargs["stderr"] is subprocess.PIPE) self.assertEquals("some text", self.__dir.read("filename")) self.assertEquals("error text", log.getvalue()) self.assertEquals("foo > filename\n", _print_command(command)) # Redirect stderr. command = SubprocessCommand(self.__action, ["foo"], capture_stderr = self.__artifact) context = MockCommandContext(self.__dir) context.subprocess_result = (0, "some text", "error text") log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) # TODO(kenton): Uncomment when bug with writing to log is solved. #self.assertTrue(context.subprocess_kwargs["stdout"] is log) self.assertTrue(context.subprocess_kwargs["stdout"] is subprocess.PIPE) self.assertTrue(context.subprocess_kwargs["stderr"] is subprocess.PIPE) self.assertEquals("some text", log.getvalue()) self.assertEquals("error text", self.__dir.read("filename")) self.assertEquals("foo 2> filename\n", _print_command(command)) # Redirect both. command = SubprocessCommand(self.__action, ["foo"], capture_stdout = self.__artifact, capture_stderr = Artifact("file2", None)) context = MockCommandContext(self.__dir) context.subprocess_result = (0, "output", "error") log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertTrue(context.subprocess_kwargs["stdout"] is subprocess.PIPE) self.assertTrue(context.subprocess_kwargs["stderr"] is subprocess.PIPE) self.assertEquals("output", self.__dir.read("filename")) self.assertEquals("error", self.__dir.read("file2")) self.assertEquals("foo > filename 2> file2\n", _print_command(command)) # Redirect both to same destination. command = SubprocessCommand(self.__action, ["foo"], capture_stdout = self.__artifact, capture_stderr = self.__artifact) context = MockCommandContext(self.__dir) context.subprocess_result = (0, "combined text", None) log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertTrue(context.subprocess_kwargs["stdout"] is subprocess.PIPE) self.assertTrue(context.subprocess_kwargs["stderr"] is subprocess.STDOUT) self.assertEquals("combined text", self.__dir.read("filename")) self.assertEquals("foo > filename 2>&1\n", _print_command(command)) def testExitStatus(self): command = SubprocessCommand(self.__action, ["foo"]) context = MockCommandContext(self.__dir) context.subprocess_result = (0, "", None) self.assertTrue(command.run(context, cStringIO.StringIO())) context = MockCommandContext(self.__dir) context.subprocess_result = (1, "", None) self.assertFalse(command.run(context, cStringIO.StringIO())) context = MockCommandContext(self.__dir) context.subprocess_result = (-1, "", None) self.assertFalse(command.run(context, cStringIO.StringIO())) # Redirect exit status. command = SubprocessCommand(self.__action, ["foo"], capture_exit_status = self.__artifact) self.assertEquals("foo && echo true > filename || echo false > filename\n", _print_command(command)) context = MockCommandContext(self.__dir) context.subprocess_result = (0, "", None) self.assertTrue(command.run(context, cStringIO.StringIO())) self.assertEquals("true", self.__dir.read("filename")) context = MockCommandContext(self.__dir) context.subprocess_result = (1, "", None) self.assertTrue(command.run(context, cStringIO.StringIO())) self.assertEquals("false", self.__dir.read("filename"))
class Configuration(object): def __init__(self, output_path, all_configs = None): # We want to make sure to construct only one copy of each config, even # if configs refer to each other or multiple configs refer to a shared # config. So, all_configs maps names to configs that we have already # constructed. if all_configs is None: # Note that if we just make all_configs default to {} in the method # signature, then Python will create a single empty map to use as the # default value for all calls rather than create a new one every call. # Since we modify all_configs during this method, we would be modifying # the shared default value, which would be bad. If you don't understand # what I mean, try typing the following into the interpreter and then # calling it several times with no argument: # def f(l = []): # l.append("foo") # return l # Ouchies. all_configs = {} if output_path is None: all_configs[""] = self else: all_configs[output_path] = self self.name = output_path self.source_dir = DiskDirectory(".") if output_path is None: self.output_dir = self.source_dir else: self.source_dir.mkdir(output_path) self.output_dir = DiskDirectory(output_path) self.mem_dir = VirtualDirectory() self.env_dir = VirtualDirectory() _restore_pickle(self.mem_dir, self.output_dir, "mem.pickle") _restore_pickle(self.env_dir, self.output_dir, "env.pickle") self.alt_configs = {} self.__make_root_dir() self.alt_configs["host"] = self if self.env_dir.exists("$mappings"): mappings = self.env_dir.read("$mappings").split(":") for mapping in mappings: if mapping == "": continue alias, name = mapping.split("=", 1) if name in all_configs: self.alt_configs[alias] = all_configs[name] else: if name == "": name = None self.alt_configs[alias] = Configuration(name, all_configs) def __make_root_dir(self): self.mapping = _WorkingDirMapping(self.source_dir, self.output_dir, self.mem_dir, self.env_dir, self.alt_configs) self.root_dir = MappedDirectory(self.mapping) def save(self): if not self.mem_dir.empty(): _save_pickle(self.mem_dir, self.root_dir, "mem.pickle") if not self.env_dir.empty(): _save_pickle(self.env_dir, self.root_dir, "env.pickle") def getenv(self, name): if self.root_dir.read("env/set/" + name) == "true": return self.root_dir.read("env/" + name) else: return None def clean(self, expunge=False): for dir in ["tmp", "bin", "lib", "share", "include", "mem", "env"]: if self.root_dir.exists(dir): shutil.rmtree(self.root_dir.get_disk_path(dir)) for file in [ "mem.pickle", "env.pickle" ]: if self.root_dir.exists(file): os.remove(self.root_dir.get_disk_path(file)) self.mem_dir = VirtualDirectory() if expunge: # Try to remove the output directory itself -- will fail if not empty. outdir = self.root_dir.get_disk_path(".") if outdir.endswith("/."): # rmdir doesn't like a trailing "/.". outdir = outdir[:-2] try: os.rmdir(outdir) except os.error: pass else: # Restore the parts of env.pickle that were set explicitly. new_env_dir = VirtualDirectory() if self.env_dir.exists("$mappings"): new_env_dir.write("$mappings", self.env_dir.read("$mappings")) if self.env_dir.exists("$config"): locked_vars = self.env_dir.read("$config") new_env_dir.write("$config", locked_vars) for var in locked_vars.split(","): if var != "": new_env_dir.write(var, self.env_dir.read(var)) new_env_dir.write("set/" + var, self.env_dir.read("set/" + var)) self.env_dir = new_env_dir self.__make_root_dir() def get_all_linked_configs(self): result = set() self.__get_all_linked_configs_recursive(result) return result def __get_all_linked_configs_recursive(self, result): if self in result: return result.add(self) for link in self.alt_configs.values(): link.__get_all_linked_configs_recursive(result)
class SubprocessCommandTest(unittest.TestCase): def setUp(self): self.__action = Action(None, "dummy", "dummy") self.__artifact = Artifact("filename", None) self.__dir = VirtualDirectory() def testVerifyArgs(self): action = self.__action artifact = self.__artifact self.assertRaises(TypeError, SubprocessCommand, action, [0]) self.assertRaises(TypeError, SubprocessCommand, action, [[0]]) self.assertRaises(TypeError, SubprocessCommand, action, [artifact, 0]) SubprocessCommand(action, ["foo"]) SubprocessCommand(action, [artifact]) SubprocessCommand(action, [artifact]) SubprocessCommand(action, [[artifact]]) SubprocessCommand(action, [[artifact]]) SubprocessCommand(action, [artifact.contents()]) def testEnumerateArtifacts(self): action = self.__action inputs = [Artifact("input1", None), Artifact("input2", None)] outputs = [Artifact("output1", action), Artifact("output2", action)] command = SubprocessCommand(action, ["foo", inputs[0], outputs[0]], implicit=[inputs[1], outputs[1]]) enumerator = MockArtifactEnumerator() command.enumerate_artifacts(enumerator) self.assertEquals([], enumerator.reads) self.assertEquals(set(inputs), set(enumerator.inputs)) self.assertEquals(set(outputs), set(enumerator.outputs)) stdout = Artifact("stdout", None) stderr = Artifact("stderr", None) exit_code = Artifact("error_code", None) command = SubprocessCommand(action, inputs + outputs, capture_stdout=stdout, capture_stderr=stderr, capture_exit_status=exit_code) enumerator = MockArtifactEnumerator() command.enumerate_artifacts(enumerator) self.assertEquals([], enumerator.reads) self.assertEquals(set(inputs), set(enumerator.inputs)) self.assertEquals(set(outputs + [stdout, stderr, exit_code]), set(enumerator.outputs)) def testFormatArgs(self): artifact = self.__artifact self.__dir.write(self.__artifact.filename, "content") self.assertFormattedAs(["foo"], ["foo"]) self.assertFormattedAs([artifact], ["disk/filename"], "filename") self.assertFormattedAs([artifact.contents()], ["content"], "$(filename)") self.assertFormattedAs([["foo"]], ["foo"]) self.assertFormattedAs([[artifact]], ["disk/filename"], "filename") self.assertFormattedAs([[artifact.contents()]], ["content"], "$(filename)") self.assertFormattedAs( ["foo", ["bar", ["baz", "qux"], "corge"], "grault"], ["foo", "barbazquxcorge", "grault"]) self.assertFormattedAs(["(", artifact, ")"], ["(", "disk/filename", ")"], "( filename )") self.assertFormattedAs([["(", artifact, ")"]], ["(disk/filename)"], "(filename)") self.__dir.write(self.__artifact.filename, "content with\nspaces") self.assertFormattedAs(["(", artifact.contents(), ")"], ["(", "content", "with", "spaces", ")"], "( $(filename) )") self.assertFormattedAs([["(", artifact.contents(), ")"]], ["(content with\nspaces)"], "($(filename))") self.assertFormattedAs( ["(", SubprocessCommand.Quoted(["foo bar", "baz"]), ")"], ["(", "'foo bar' baz", ")"]) self.assertFormattedAs([SubprocessCommand.Quoted(["'hello'"])], ["\"'hello'\""]) self.assertFormattedAs( [SubprocessCommand.Quoted(["(", artifact, ")"])], ["'(' disk/filename ')'"], "'(' filename ')'") def assertFormattedAs(self, args, result, printed=None): context = MockCommandContext(self.__dir, diskpath_prefix="disk/") command = SubprocessCommand(self.__action, list(args)) self.assertTrue(command.run(context, cStringIO.StringIO())) self.assertEquals(result, context.subprocess_args) if printed is None: printed = " ".join(result) self.assertEquals(printed + "\n", _print_command(command)) def testRedirectStreams(self): # No redirection. command = SubprocessCommand(self.__action, ["foo"]) context = MockCommandContext(self.__dir) context.subprocess_result = (0, "some text", None) log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertTrue(context.subprocess_kwargs["stdout"] is subprocess.PIPE) self.assertTrue( context.subprocess_kwargs["stderr"] is subprocess.STDOUT) self.assertEquals("some text", log.getvalue()) self.assertEquals("foo\n", _print_command(command)) # Redirect stdout. command = SubprocessCommand(self.__action, ["foo"], capture_stdout=self.__artifact) context = MockCommandContext(self.__dir) context.subprocess_result = (0, "some text", "error text") log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertTrue(context.subprocess_kwargs["stdout"] is subprocess.PIPE) self.assertTrue(context.subprocess_kwargs["stderr"] is subprocess.PIPE) self.assertEquals("some text", self.__dir.read("filename")) self.assertEquals("error text", log.getvalue()) self.assertEquals("foo > filename\n", _print_command(command)) # Redirect stderr. command = SubprocessCommand(self.__action, ["foo"], capture_stderr=self.__artifact) context = MockCommandContext(self.__dir) context.subprocess_result = (0, "some text", "error text") log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) # TODO(kenton): Uncomment when bug with writing to log is solved. #self.assertTrue(context.subprocess_kwargs["stdout"] is log) self.assertTrue(context.subprocess_kwargs["stdout"] is subprocess.PIPE) self.assertTrue(context.subprocess_kwargs["stderr"] is subprocess.PIPE) self.assertEquals("some text", log.getvalue()) self.assertEquals("error text", self.__dir.read("filename")) self.assertEquals("foo 2> filename\n", _print_command(command)) # Redirect both. command = SubprocessCommand(self.__action, ["foo"], capture_stdout=self.__artifact, capture_stderr=Artifact("file2", None)) context = MockCommandContext(self.__dir) context.subprocess_result = (0, "output", "error") log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertTrue(context.subprocess_kwargs["stdout"] is subprocess.PIPE) self.assertTrue(context.subprocess_kwargs["stderr"] is subprocess.PIPE) self.assertEquals("output", self.__dir.read("filename")) self.assertEquals("error", self.__dir.read("file2")) self.assertEquals("foo > filename 2> file2\n", _print_command(command)) # Redirect both to same destination. command = SubprocessCommand(self.__action, ["foo"], capture_stdout=self.__artifact, capture_stderr=self.__artifact) context = MockCommandContext(self.__dir) context.subprocess_result = (0, "combined text", None) log = cStringIO.StringIO() self.assertTrue(command.run(context, log)) self.assertTrue(context.subprocess_kwargs["stdout"] is subprocess.PIPE) self.assertTrue( context.subprocess_kwargs["stderr"] is subprocess.STDOUT) self.assertEquals("combined text", self.__dir.read("filename")) self.assertEquals("foo > filename 2>&1\n", _print_command(command)) def testExitStatus(self): command = SubprocessCommand(self.__action, ["foo"]) context = MockCommandContext(self.__dir) context.subprocess_result = (0, "", None) self.assertTrue(command.run(context, cStringIO.StringIO())) context = MockCommandContext(self.__dir) context.subprocess_result = (1, "", None) self.assertFalse(command.run(context, cStringIO.StringIO())) context = MockCommandContext(self.__dir) context.subprocess_result = (-1, "", None) self.assertFalse(command.run(context, cStringIO.StringIO())) # Redirect exit status. command = SubprocessCommand(self.__action, ["foo"], capture_exit_status=self.__artifact) self.assertEquals( "foo && echo true > filename || echo false > filename\n", _print_command(command)) context = MockCommandContext(self.__dir) context.subprocess_result = (0, "", None) self.assertTrue(command.run(context, cStringIO.StringIO())) self.assertEquals("true", self.__dir.read("filename")) context = MockCommandContext(self.__dir) context.subprocess_result = (1, "", None) self.assertTrue(command.run(context, cStringIO.StringIO())) self.assertEquals("false", self.__dir.read("filename"))