def test_command_string(self): """Test that command string matches expected.""" args = (self.expected_base_options + self.expected_input_options + self.expected_filter_options + self.expected_output_options) expected_cmd_unquoted = ("{exe} {arg}".format( exe=self.expected_executable, arg=" ".join(args))) expected_cmd_quoted = ('{exe} {arg}'.format( exe=ShellCommand.shellquote(self.expected_executable), arg=" ".join([ShellCommand.shellquote(a) for a in args]))) with self.subTest(msg="unquoted command string matches"): self.assertEqual(self.command.command_string(quote=False), expected_cmd_unquoted) with self.subTest(msg="quoted command string matches"): self.assertEqual(self.command.command_string(quote=True), expected_cmd_quoted)
def test_shellquote(self): """Test shell quoting (static method).""" test_data = ( (None, None, "None ⇒ None"), ("", "", "(empty string) ⇒ (empty string)"), (" ", '" "', '␢ ⇒ " "'), (" ", '" "', '␢␢␢␢␢ ⇒ " "'), ("foobar", "foobar", "foobar ⇒ foobar"), ("foo bar", '"foo bar"', 'foo bar ⇒ "foo bar"'), ('"foobar"', '\'"foobar"\'', '"foobar" ⇒ \'"foobar"\''), ("'foobar'", "'foobar'", "'foobar' ⇒ 'foobar'"), ("foo 'bar'", '"foo \'bar\'"', "foo 'bar' ⇒ \"foo 'bar'\""), ('foo"bar', '\'foo"bar\'', 'foo"bar ⇒ \'foo"bar\''), ("foo.bar", '"foo.bar"', 'foo.bar ⇒ "foo.bar"'), ("foo(bar)", '"foo(bar)"', 'foo(bar) ⇒ "foo(bar)"'), ("[foobar]", '"[foobar]"', '[foobar] ⇒ "[foobar]"'), ("foo[bar", '"foo[bar"', 'foo[bar ⇒ "foo[bar"'), ("/foo/bar", "/foo/bar", "/foo/bar ⇒ /foo/bar"), ("-f", "-f", "-f ⇒ -f"), ("--foobar", "--foobar", "--foobar ⇒ --foobar"), ("(", r"\(", r"( ⇒ \("), (")", r"\)", r"( ⇒ \)"), ("'", '"\'"', '\' ⇒ "\'"'), ) for original, expected, description in test_data: with self.subTest(msg=description): self.assertEqual(ShellCommand.shellquote(original), expected)
def test_executable_string(self): """Test that executable path matches expected.""" with self.subTest(msg="unquoted paths match"): self.assertEqual(self.command.executable_string(quote=False), self.expected_executable) with self.subTest(msg="unquoted paths match"): # Note: don't explicitly specify quoted value, because # the executable path will vary across different systems. self.assertEqual(self.command.executable_string(quote=True), ShellCommand.shellquote(self.expected_executable))
def test_argument_string(self): """Test that argument string matches expected.""" args = (self.expected_base_options + self.expected_input_options + self.expected_filter_options + self.expected_output_options) with self.subTest(msg="unquoted agruments match"): self.assertEqual(self.command.argument_string(quote=False), " ".join(args)) with self.subTest(msg="quoted agruments match"): self.assertEqual( self.command.argument_string(quote=True), " ".join([ShellCommand.shellquote(a) for a in args]))