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 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 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"))
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 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"))
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 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 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 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))