def testFlagWithEqualGreaterThanShouldIgnoreIntervalFlags(self): args, output_path = command_parser.extract_output_file_path( ["lp", "--execution_time=>100ms"]) self.assertEqual(["lp", "--execution_time=>100ms"], args) self.assertIsNone(output_path) args, output_path = command_parser.extract_output_file_path( ["lp", "--execution_time", ">1.2s"]) self.assertEqual(["lp", "--execution_time", ">1.2s"], args) self.assertIsNone(output_path) args, output_path = command_parser.extract_output_file_path( ["lp", "-e", ">1200"]) self.assertEqual(["lp", "-e", ">1200"], args) self.assertIsNone(output_path) args, output_path = command_parser.extract_output_file_path( ["lp", "--foo_value", ">-.2MB"]) self.assertEqual(["lp", "--foo_value", ">-.2MB"], args) self.assertIsNone(output_path) args, output_path = command_parser.extract_output_file_path( ["lp", "--bar_value", ">-42e3GB"]) self.assertEqual(["lp", "--bar_value", ">-42e3GB"], args) self.assertIsNone(output_path) args, output_path = command_parser.extract_output_file_path( ["lp", "--execution_time", ">=100ms"]) self.assertEqual(["lp", "--execution_time", ">=100ms"], args) self.assertIsNone(output_path) args, output_path = command_parser.extract_output_file_path( ["lp", "--execution_time=>=100ms"]) self.assertEqual(["lp", "--execution_time=>=100ms"], args) self.assertIsNone(output_path)
def testFlagWithEqualGreaterThanShouldRecognizeFilePaths(self): args, output_path = command_parser.extract_output_file_path( ["lp", ">1.2s"]) self.assertEqual(["lp"], args) self.assertEqual("1.2s", output_path) args, output_path = command_parser.extract_output_file_path( ["lp", "--execution_time", ">x.yms"]) self.assertEqual(["lp", "--execution_time"], args) self.assertEqual("x.yms", output_path) args, output_path = command_parser.extract_output_file_path( ["lp", "--memory", ">a.1kB"]) self.assertEqual(["lp", "--memory"], args) self.assertEqual("a.1kB", output_path) args, output_path = command_parser.extract_output_file_path( ["lp", "--memory", ">e002MB"]) self.assertEqual(["lp", "--memory"], args) self.assertEqual("e002MB", output_path)
def _parse_command(self, command): """Parse a command string into prefix and arguments. Args: command: (str) Command string to be parsed. Returns: prefix: (str) The command prefix. args: (list of str) The command arguments (i.e., not including the prefix). output_file_path: (str or None) The path to save the screen output to (if any). """ command = command.strip() if not command: return "", [], None command_items = command_parser.parse_command(command) command_items, output_file_path = command_parser.extract_output_file_path( command_items) return command_items[0], command_items[1:], output_file_path
def testOutputPathInLastArgGreaterThanInSecondLastIsHandledCorrectly(self): args, output_path = command_parser.extract_output_file_path( ["pt", "a:0>", "/tmp/foo.txt"]) self.assertEqual(["pt", "a:0"], args) self.assertEqual(output_path, "/tmp/foo.txt")
def testOutputPathMergedWithLastArgIsHandledCorrectly(self): args, output_path = command_parser.extract_output_file_path( ["pt", "a:0>/tmp/foo.txt"]) self.assertEqual(["pt", "a:0"], args) self.assertEqual(output_path, "/tmp/foo.txt")
def testHasGreaterThanSignButNoFileNameCausesSyntaxError(self): with self.assertRaisesRegexp(SyntaxError, "Redirect file path is empty"): command_parser.extract_output_file_path( ["pt", "a:0", ">"])
def testHasOutputFilePathInTwoArgsIsReflected(self): args, output_path = command_parser.extract_output_file_path( ["pt", "a:0", ">", "/tmp/foo.txt"]) self.assertEqual(["pt", "a:0"], args) self.assertEqual(output_path, "/tmp/foo.txt")
def testNoOutputFilePathIsReflected(self): args, output_path = command_parser.extract_output_file_path(["pt", "a:0"]) self.assertEqual(["pt", "a:0"], args) self.assertIsNone(output_path)
def testEmptyArgumentIsHandledCorrectly(self): args, output_path = command_parser.extract_output_file_path([]) self.assertEqual([], args) self.assertIsNone(output_path)
def testHasGreaterThanSignButNoFileNameCausesSyntaxError(self): with self.assertRaisesRegexp(SyntaxError, "Redirect file path is empty"): command_parser.extract_output_file_path(["pt", "a:0", ">"])
def testNoOutputFilePathIsReflected(self): args, output_path = command_parser.extract_output_file_path( ["pt", "a:0"]) self.assertEqual(["pt", "a:0"], args) self.assertIsNone(output_path)