示例#1
0
  def checkstyle(self, sources, targets):
    egroups = self.context.products.get_data('exclusives_groups')
    etag = egroups.get_group_key_for_target(targets[0])
    classpath = self.tool_classpath(self._checkstyle_bootstrap_key)
    cp = egroups.get_classpath_for_group(etag)
    classpath.extend(jar for conf, jar in cp if conf in self._confs)

    args = [
      '-c', self._configuration_file,
      '-f', 'plain'
    ]

    if self._properties:
      properties_file = os.path.join(self.workdir, 'checkstyle.properties')
      with safe_open(properties_file, 'w') as pf:
        for k, v in self._properties.items():
          pf.write('%s=%s\n' % (k, v))
      args.extend(['-p', properties_file])

    # We've hit known cases of checkstyle command lines being too long for the system so we guard
    # with Xargs since checkstyle does not accept, for example, @argfile style arguments.
    def call(xargs):
      return self.runjava(classpath=classpath, main=CHECKSTYLE_MAIN,
                          args=args + xargs, workunit_name='checkstyle')
    checks = Xargs(call)

    return checks.execute(sources)
示例#2
0
    def checkstyle(self, sources, targets):
        egroups = self.context.products.get_data('exclusives_groups')
        etag = egroups.get_group_key_for_target(targets[0])
        classpath = self.tool_classpath(self._checkstyle_bootstrap_key)
        cp = egroups.get_classpath_for_group(etag)
        classpath.extend(jar for conf, jar in cp if conf in self._confs)

        args = ['-c', self._configuration_file, '-f', 'plain']

        if self._properties:
            properties_file = os.path.join(self.workdir,
                                           'checkstyle.properties')
            with safe_open(properties_file, 'w') as pf:
                for k, v in self._properties.items():
                    pf.write('%s=%s\n' % (k, v))
            args.extend(['-p', properties_file])

        # We've hit known cases of checkstyle command lines being too long for the system so we guard
        # with Xargs since checkstyle does not accept, for example, @argfile style arguments.
        def call(xargs):
            return self.runjava(classpath=classpath,
                                main=CHECKSTYLE_MAIN,
                                args=args + xargs,
                                workunit_name='checkstyle')

        checks = Xargs(call)

        return checks.execute(sources)
示例#3
0
  def checkstyle(self, targets, sources):
    runtime_classpaths = self.context.products.get_data('runtime_classpath')
    runtime_classpath = runtime_classpaths.get_for_targets(targets)
    union_classpath = OrderedSet(self.tool_classpath('checkstyle'))
    union_classpath.update(jar for conf, jar in runtime_classpath if conf in self.get_options().confs)

    args = [
      '-c', self.get_options().configuration,
      '-f', 'plain'
    ]

    if self.get_options().properties:
      properties_file = os.path.join(self.workdir, 'checkstyle.properties')
      with safe_open(properties_file, 'w') as pf:
        for k, v in self.get_options().properties.items():
          pf.write('{key}={value}\n'.format(key=k, value=v))
      args.extend(['-p', properties_file])

    # We've hit known cases of checkstyle command lines being too long for the system so we guard
    # with Xargs since checkstyle does not accept, for example, @argfile style arguments.
    def call(xargs):
      return self.runjava(classpath=union_classpath, main=self._CHECKSTYLE_MAIN,
                          jvm_options=self.get_options().jvm_options,
                          args=args + xargs, workunit_name='checkstyle')
    checks = Xargs(call)

    return checks.execute(sources)
示例#4
0
  def checkstyle(self, targets, sources):
    runtime_classpaths = self.context.products.get_data('runtime_classpath')
    union_classpath = OrderedSet(self.tool_classpath('checkstyle'))
    for target in targets:
      runtime_classpath = runtime_classpaths.get_for_targets(target.closure(bfs=True))
      union_classpath.update(jar for conf, jar in runtime_classpath
                             if conf in self.get_options().confs)

    args = [
      '-c', self.get_options().configuration,
      '-f', 'plain'
    ]

    if self.get_options().properties:
      properties_file = os.path.join(self.workdir, 'checkstyle.properties')
      with safe_open(properties_file, 'w') as pf:
        for k, v in self.get_options().properties.items():
          pf.write('{key}={value}\n'.format(key=k, value=v))
      args.extend(['-p', properties_file])

    # We've hit known cases of checkstyle command lines being too long for the system so we guard
    # with Xargs since checkstyle does not accept, for example, @argfile style arguments.
    def call(xargs):
      return self.runjava(classpath=union_classpath, main=self._CHECKSTYLE_MAIN,
                          jvm_options=self.get_options().jvm_options,
                          args=args + xargs, workunit_name='checkstyle')
    checks = Xargs(call)

    return checks.execute(sources)
示例#5
0
  def execute(self):
    if self.context.options.scalastyle_skip:
      self.context.log.debug('Skipping checkstyle.')
      return

    check_targets = list()
    targets = self.context.targets()
    for target in targets:
      for tgt in target.resolve():
        if isinstance(tgt, Target) and tgt.has_sources('.scala'):
          check_targets.append(tgt)

    def filter_excludes(filename):
      if self._excludes:
        for exclude in self._excludes:
          if exclude.match(filename):
            return False
      return True

    scala_sources = list()
    for target in check_targets:
      def collect(filename):
        if filename.endswith('.scala'):
          scala_sources.append(os.path.join(target.target_base, filename))
      map(collect, filter(filter_excludes, target.sources))

    if scala_sources:
      def call(srcs):
        cp = self.tool_classpath(self._scalastyle_bootstrap_key)
        return self.runjava(classpath=cp,
                            main=Scalastyle._MAIN,
                            args=['-c', self._scalastyle_config] + srcs)
      result = Xargs(call).execute(scala_sources)
      if result != 0:
        raise TaskError('java %s ... exited non-zero (%i)' % (Scalastyle._MAIN, result))
示例#6
0
    def execute(self):
        if self.get_options().skip:
            self.context.log.info('Skipping scalastyle.')
            return

        # Don't even try and validate options if we're irrelevant.
        targets = self.get_non_synthetic_scala_targets(self.context.targets())
        if not targets:
            return

        with self.invalidated(targets) as invalidation_check:
            invalid_targets = [
                vt.target for vt in invalidation_check.invalid_vts
            ]

            scalastyle_config = self.validate_scalastyle_config()

            scalastyle_verbose = self.get_options().verbose
            scalastyle_quiet = self.get_options().quiet or False
            scalastyle_excluder = self.create_file_excluder()

            self.context.log.debug(
                'Non synthetic scala targets to be checked:')
            for target in invalid_targets:
                self.context.log.debug('  {address_spec}'.format(
                    address_spec=target.address.spec))

            scala_sources = self.get_non_excluded_scala_sources(
                scalastyle_excluder, invalid_targets)
            self.context.log.debug('Non excluded scala sources to be checked:')
            for source in scala_sources:
                self.context.log.debug('  {source}'.format(source=source))

            if scala_sources:

                def call(srcs):
                    def to_java_boolean(x):
                        return str(x).lower()

                    cp = ScalaPlatform.global_instance().style_classpath(
                        self.context.products)
                    scalastyle_args = [
                        '-c',
                        scalastyle_config,
                        '-v',
                        to_java_boolean(scalastyle_verbose),
                        '-q',
                        to_java_boolean(scalastyle_quiet),
                    ]
                    return self.runjava(
                        classpath=cp,
                        main=self._MAIN,
                        jvm_options=self.get_options().jvm_options,
                        args=scalastyle_args + srcs)

                result = Xargs(call).execute(scala_sources)
                if result != 0:
                    raise TaskError(
                        'java {entry} ... exited non-zero ({exit_code})'.
                        format(entry=Scalastyle._MAIN, exit_code=result))
示例#7
0
    def execute(self):
        if self._should_skip:
            self.context.log.info('Skipping scalastyle.')
            return

        targets = self._get_non_synthetic_scala_targets(self.context.targets())
        self.context.log.debug('Non synthetic scala targets to be checked:')
        for target in targets:
            self.context.log.debug(
                '  {address_spec}'.format(address_spec=target.address.spec))

        scala_sources = self._get_non_excluded_scala_sources(targets)
        self.context.log.debug('Non excluded scala sources to be checked:')
        for source in scala_sources:
            self.context.log.debug('  {source}'.format(source=source))

        if scala_sources:

            def call(srcs):
                cp = self.tool_classpath(self._scalastyle_bootstrap_key)
                return self.runjava(classpath=cp,
                                    main=self._MAIN,
                                    args=['-c', self._scalastyle_config] +
                                    srcs)

            result = Xargs(call).execute(scala_sources)
            if result != 0:
                raise TaskError(
                    'java {entry} ... exited non-zero ({exit_code})'.format(
                        entry=Scalastyle._MAIN, exit_code=result))
示例#8
0
    def checkstyle(self, targets, sources):
        union_classpath = OrderedSet(self.tool_classpath('checkstyle'))
        if self.get_options().include_user_classpath:
            runtime_classpaths = self.context.products.get_data(
                'runtime_classpath')
            for target in targets:
                runtime_classpath = runtime_classpaths.get_for_targets(
                    target.closure(bfs=True))
                union_classpath.update(jar for conf, jar in runtime_classpath
                                       if conf in self.get_options().confs)

        task_config = self.get_options().configuration
        subsystem_config = CheckstyleSubsystem.global_instance().options.config
        if task_config and subsystem_config:
            raise ValueError(
                "Conflicting options for the config file used. You used the new, preferred "
                "`--checkstyle-config`, but also used the deprecated `--lint-checkstyle-configuration`.\n"
                "Please use only one of these (preferably `--checkstyle-config`)."
            )
        config = task_config or subsystem_config
        if not config:
            raise TaskError(
                'No checkstyle configuration file configured. Configure with `--checkstyle-config`.'
            )

        args = ['-c', config, '-f', 'plain']

        if self.get_options().properties:
            properties_file = os.path.join(self.workdir,
                                           'checkstyle.properties')
            with safe_open(properties_file, 'w') as pf:
                for k, v in self.get_options().properties.items():
                    pf.write(f'{k}={v}\n')
            args.extend(['-p', properties_file])

        # We've hit known cases of checkstyle command lines being too long for the system so we guard
        # with Xargs since checkstyle does not accept, for example, @argfile style arguments.
        def call(xargs):
            return self.runjava(classpath=union_classpath,
                                main=self._CHECKSTYLE_MAIN,
                                jvm_options=self.get_options().jvm_options,
                                args=args + xargs,
                                workunit_name='checkstyle')

        checks = Xargs(call)

        return checks.execute(sources)
示例#9
0
  def _execute_for(self, targets):
    target_sources = self._calculate_sources(targets)
    if not target_sources:
      return

    result = Xargs(self._invoke_tool_in_place).execute(target_sources)
    if result != 0:
      raise TaskError('{} is improperly implemented: a failed process '
                      'should raise an exception earlier.'.format(type(self).__name__))
示例#10
0
文件: go_run.py 项目: thoward/pants
 def execute(self):
   target = self.require_single_root_target()
   if self.is_binary(target):
     binary_path = self.context.products.get_data('exec_binary')[target]
     # TODO(cgibb): Wrap with workunit and stdout/stderr plumbing.
     res = Xargs.subprocess([binary_path]).execute(self.get_passthru_args())
     if res != 0:
       raise TaskError('{bin} exited non-zero ({res})'
                       .format(bin=os.path.basename(binary_path), res=res))
示例#11
0
文件: go_run.py 项目: scalameta/pants
 def execute(self):
     target = self.require_single_root_target()
     if self.is_binary(target):
         binary_path = self.context.products.get_data("exec_binary")[target]
         # TODO(cgibb): Wrap with workunit and stdout/stderr plumbing.
         res = Xargs.subprocess([binary_path]).execute(
             [*self.get_passthru_args(), *self.get_options().args]
         )
         if res != 0:
             raise TaskError(f"{os.path.basename(binary_path)} exited non-zero ({res})")
示例#12
0
文件: checkstyle.py 项目: wiwa/pants
    def checkstyle(self, targets, sources):
        union_classpath = OrderedSet(self.tool_classpath("checkstyle"))
        if self.get_options().include_user_classpath:
            runtime_classpaths = self.context.products.get_data("runtime_classpath")
            for target in targets:
                runtime_classpath = runtime_classpaths.get_for_targets(target.closure(bfs=True))
                union_classpath.update(
                    jar for conf, jar in runtime_classpath if conf in self.get_options().confs
                )

        config = CheckstyleSubsystem.global_instance().options.config
        if not config:
            raise TaskError(
                "No checkstyle configuration file configured. Configure with `--checkstyle-config`."
            )

        args = ["-c", config, "-f", "plain"]

        if self.get_options().properties:
            properties_file = os.path.join(self.workdir, "checkstyle.properties")
            with safe_open(properties_file, "w") as pf:
                for k, v in self.get_options().properties.items():
                    pf.write(f"{k}={v}\n")
            args.extend(["-p", properties_file])

        # We've hit known cases of checkstyle command lines being too long for the system so we guard
        # with Xargs since checkstyle does not accept, for example, @argfile style arguments.
        def call(xargs):
            return self.runjava(
                classpath=union_classpath,
                main=self._CHECKSTYLE_MAIN,
                jvm_options=self.get_options().jvm_options,
                args=args + xargs,
                workunit_name="checkstyle",
            )

        checks = Xargs(call)

        return checks.execute(sources)
示例#13
0
    def execute(self):
        # Don't even try and validate options if we're irrelevant.
        targets = self.get_non_synthetic_scala_targets(self.get_targets())
        if not targets:
            return

        with self.invalidated(targets) as invalidation_check:
            invalid_targets = [vt.target for vt in invalidation_check.invalid_vts]

            scalastyle_config = self.validate_scalastyle_config()

            scalastyle_verbose = self.get_options().verbose
            scalastyle_quiet = self.get_options().quiet or False
            scalastyle_excluder = self.create_file_excluder()

            self.context.log.debug("Non synthetic scala targets to be checked:")
            for target in invalid_targets:
                self.context.log.debug(f"  {target.address.spec}")

            scala_sources = self.get_non_excluded_scala_sources(
                scalastyle_excluder, invalid_targets
            )
            self.context.log.debug("Non excluded scala sources to be checked:")
            for source in scala_sources:
                self.context.log.debug(f"  {source}")

            if scala_sources:

                def call(srcs):
                    def to_java_boolean(x):
                        return str(x).lower()

                    cp = ScalaPlatform.global_instance().style_classpath(self.context.products)
                    scalastyle_args = [
                        "-c",
                        scalastyle_config,
                        "-v",
                        to_java_boolean(scalastyle_verbose),
                        "-q",
                        to_java_boolean(scalastyle_quiet),
                    ]
                    return self.runjava(
                        classpath=cp,
                        main=self._MAIN,
                        jvm_options=self.get_options().jvm_options,
                        args=scalastyle_args + srcs,
                    )

                result = Xargs(call).execute(scala_sources)
                if result != 0:
                    raise TaskError(f"java {ScalastyleTask._MAIN} ... exited non-zero ({result})")
示例#14
0
  def checkstyle(self, targets, sources):
    union_classpath = OrderedSet(self.tool_classpath('checkstyle'))
    if self.get_options().include_user_classpath:
      runtime_classpaths = self.context.products.get_data('runtime_classpath')
      for target in targets:
        runtime_classpath = runtime_classpaths.get_for_targets(target.closure(bfs=True))
        union_classpath.update(jar for conf, jar in runtime_classpath
                               if conf in self.get_options().confs)

    config = self._resolve_conflicting_options(old_option="configuration", new_option="config")
    if not config:
      raise TaskError(
        'No checkstyle configuration file configured. Configure with `--checkstyle-config`.'
      )

    args = [
      '-c', config,
      '-f', 'plain'
    ]

    if self.get_options().properties:
      properties_file = os.path.join(self.workdir, 'checkstyle.properties')
      with safe_open(properties_file, 'w') as pf:
        for k, v in self.get_options().properties.items():
          pf.write(f'{k}={v}\n')
      args.extend(['-p', properties_file])

    # We've hit known cases of checkstyle command lines being too long for the system so we guard
    # with Xargs since checkstyle does not accept, for example, @argfile style arguments.
    def call(xargs):
      return self.runjava(classpath=union_classpath, main=self._CHECKSTYLE_MAIN,
                          jvm_options=self.get_options().jvm_options,
                          args=args + xargs, workunit_name='checkstyle')
    checks = Xargs(call)

    return checks.execute(sources)
示例#15
0
    def execute(self):
        """Runs Scalafmt on all found Scala Source Files."""
        if self.get_options().skip:
            return

        targets = self.get_non_synthetic_scala_targets(self.context.targets())

        with self.invalidated(targets) as invalidation_check:
            invalid_targets = [
                vt.target for vt in invalidation_check.invalid_vts
            ]
            sources = self.calculate_sources(invalid_targets)
            if not sources:
                return

            result = Xargs(self._invoke_scalafmt).execute(sources)
            self.process_results(result)
示例#16
0
class XargsTest(unittest.TestCase):
  def setUp(self):
    self.call = mock.Mock()
    self.xargs = Xargs(self.call)

  def test_execute_nosplit_success(self):
    self.call.return_value = 0

    self.assertEqual(0, self.xargs.execute(['one', 'two', 'three', 'four']))

    self.call.assert_called_once_with(['one', 'two', 'three', 'four'])

  def test_execute_nosplit_raise(self):
    exception = Exception()
    self.call.side_effect = exception

    with self.assertRaises(Exception) as raised:
      self.xargs.execute(['one', 'two', 'three', 'four'])

    self.call.assert_called_once_with(['one', 'two', 'three', 'four'])
    self.assertIs(exception, raised.exception)

  def test_execute_nosplit_fail(self):
    self.call.return_value = 42

    self.assertEqual(42, self.xargs.execute(['one', 'two', 'three', 'four']))

    self.call.assert_called_once_with(['one', 'two', 'three', 'four'])

  TOO_BIG = OSError(errno.E2BIG, os.strerror(errno.E2BIG))

  def test_execute_split(self):
    self.call.side_effect = (self.TOO_BIG, 0, 0)

    self.assertEqual(0, self.xargs.execute(['one', 'two', 'three', 'four']))

    self.assertEqual([mock.call(['one', 'two', 'three', 'four']),
                      mock.call(['one', 'two']),
                      mock.call(['three', 'four'])],
                     self.call.mock_calls)

  def test_execute_uneven(self):
    self.call.side_effect = (self.TOO_BIG, 0, 0)

    self.assertEqual(0, self.xargs.execute(['one', 'two', 'three']))

    self.assertEqual(3, self.call.call_count)
    self.assertEqual(mock.call(['one', 'two', 'three']),
                     self.call.mock_calls[0])

    self.assertEqual(sorted((mock.call(['one']), mock.call(['two', 'three']))),
                     sorted(self.call.mock_calls[1:]))

  def test_execute_split_multirecurse(self):
    self.call.side_effect = (self.TOO_BIG, self.TOO_BIG, 0, 0, 0)

    self.assertEqual(0, self.xargs.execute(['one', 'two', 'three', 'four']))

    self.assertEqual([mock.call(['one', 'two', 'three', 'four']),
                      mock.call(['one', 'two']),
                      mock.call(['one']),
                      mock.call(['two']),
                      mock.call(['three', 'four'])],
                     self.call.mock_calls)

  def test_execute_split_fail_fast(self):
    self.call.side_effect = (self.TOO_BIG, 42)

    self.assertEqual(42, self.xargs.execute(['one', 'two', 'three', 'four']))

    self.assertEqual([mock.call(['one', 'two', 'three', 'four']),
                      mock.call(['one', 'two'])],
                     self.call.mock_calls)

  def test_execute_split_fail_slow(self):
    self.call.side_effect = (self.TOO_BIG, 0, 42)

    self.assertEqual(42, self.xargs.execute(['one', 'two', 'three', 'four']))

    self.assertEqual([mock.call(['one', 'two', 'three', 'four']),
                      mock.call(['one', 'two']),
                      mock.call(['three', 'four'])],
                     self.call.mock_calls)
示例#17
0
 def setUp(self):
   self.call = mock.Mock()
   self.xargs = Xargs(self.call)
示例#18
0
class XargsTest(unittest.TestCase):
    def setUp(self):
        self.call = unittest.mock.Mock()
        self.xargs = Xargs(self.call)

    def test_execute_nosplit_success(self):
        self.call.return_value = 0

        self.assertEqual(0, self.xargs.execute(['one', 'two', 'three',
                                                'four']))

        self.call.assert_called_once_with(['one', 'two', 'three', 'four'])

    def test_execute_nosplit_raise(self):
        exception = Exception()
        self.call.side_effect = exception

        with self.assertRaises(Exception) as raised:
            self.xargs.execute(['one', 'two', 'three', 'four'])

        self.call.assert_called_once_with(['one', 'two', 'three', 'four'])
        self.assertIs(exception, raised.exception)

    def test_execute_nosplit_fail(self):
        self.call.return_value = 42

        self.assertEqual(42,
                         self.xargs.execute(['one', 'two', 'three', 'four']))

        self.call.assert_called_once_with(['one', 'two', 'three', 'four'])

    TOO_BIG = OSError(errno.E2BIG, os.strerror(errno.E2BIG))

    def test_execute_split(self):
        self.call.side_effect = (self.TOO_BIG, 0, 0)

        self.assertEqual(0, self.xargs.execute(['one', 'two', 'three',
                                                'four']))

        self.assertEqual([
            unittest.mock.call(['one', 'two', 'three', 'four']),
            unittest.mock.call(['one', 'two']),
            unittest.mock.call(['three', 'four'])
        ], self.call.mock_calls)

    def test_execute_uneven(self):
        self.call.side_effect = (self.TOO_BIG, 0, 0)

        self.assertEqual(0, self.xargs.execute(['one', 'two', 'three']))

        self.assertEqual(3, self.call.call_count)
        self.assertEqual(unittest.mock.call(['one', 'two', 'three']),
                         self.call.mock_calls[0])

        self.assertEqual(
            sorted((unittest.mock.call(['one']),
                    unittest.mock.call(['two', 'three']))),
            sorted(self.call.mock_calls[1:]))

    def test_execute_split_multirecurse(self):
        self.call.side_effect = (self.TOO_BIG, self.TOO_BIG, 0, 0, 0)

        self.assertEqual(0, self.xargs.execute(['one', 'two', 'three',
                                                'four']))

        self.assertEqual([
            unittest.mock.call(['one', 'two', 'three', 'four']),
            unittest.mock.call(['one', 'two']),
            unittest.mock.call(['one']),
            unittest.mock.call(['two']),
            unittest.mock.call(['three', 'four'])
        ], self.call.mock_calls)

    def test_execute_split_fail_fast(self):
        self.call.side_effect = (self.TOO_BIG, 42)

        self.assertEqual(42,
                         self.xargs.execute(['one', 'two', 'three', 'four']))

        self.assertEqual([
            unittest.mock.call(['one', 'two', 'three', 'four']),
            unittest.mock.call(['one', 'two'])
        ], self.call.mock_calls)

    def test_execute_split_fail_slow(self):
        self.call.side_effect = (self.TOO_BIG, 0, 42)

        self.assertEqual(42,
                         self.xargs.execute(['one', 'two', 'three', 'four']))

        self.assertEqual([
            unittest.mock.call(['one', 'two', 'three', 'four']),
            unittest.mock.call(['one', 'two']),
            unittest.mock.call(['three', 'four'])
        ], self.call.mock_calls)
示例#19
0
 def setUp(self):
     super(XargsTest, self).setUp()
     self.call = self.mox.CreateMockAnything()
     self.xargs = Xargs(self.call)
示例#20
0
class XargsTest(mox.MoxTestBase):
    def setUp(self):
        super(XargsTest, self).setUp()
        self.call = self.mox.CreateMockAnything()
        self.xargs = Xargs(self.call)

    def test_execute_nosplit_success(self):
        self.call(["one", "two", "three", "four"]).AndReturn(0)
        self.mox.ReplayAll()

        self.assertEqual(0, self.xargs.execute(["one", "two", "three", "four"]))

    def test_execute_nosplit_raise(self):
        exception = Exception()

        self.call(["one", "two", "three", "four"]).AndRaise(exception)
        self.mox.ReplayAll()

        with pytest.raises(Exception) as raised:
            self.xargs.execute(["one", "two", "three", "four"])
        self.assertTrue(exception is raised.value)

    def test_execute_nosplit_fail(self):
        self.call(["one", "two", "three", "four"]).AndReturn(42)
        self.mox.ReplayAll()

        self.assertEqual(42, self.xargs.execute(["one", "two", "three", "four"]))

    TOO_BIG = OSError(errno.E2BIG, os.strerror(errno.E2BIG))

    def test_execute_split(self):
        self.call(["one", "two", "three", "four"]).AndRaise(self.TOO_BIG)
        self.call(["one", "two"]).AndReturn(0)
        self.call(["three", "four"]).AndReturn(0)
        self.mox.ReplayAll()

        self.assertEqual(0, self.xargs.execute(["one", "two", "three", "four"]))

    def test_execute_uneven(self):
        self.call(["one", "two", "three"]).AndRaise(self.TOO_BIG)
        # TODO(John Sirois): We really don't care if the 1st call gets 1 argument or 2, we just
        # care that all arguments get passed just once via exactly 2 rounds of call - consider making
        # this test less brittle to changes in the chunking logic.
        self.call(["one"]).AndReturn(0)
        self.call(["two", "three"]).AndReturn(0)
        self.mox.ReplayAll()

        self.assertEqual(0, self.xargs.execute(["one", "two", "three"]))

    def test_execute_split_multirecurse(self):
        self.call(["one", "two", "three", "four"]).AndRaise(self.TOO_BIG)
        self.call(["one", "two"]).AndRaise(self.TOO_BIG)
        self.call(["one"]).AndReturn(0)
        self.call(["two"]).AndReturn(0)
        self.call(["three", "four"]).AndReturn(0)
        self.mox.ReplayAll()

        self.assertEqual(0, self.xargs.execute(["one", "two", "three", "four"]))

    def test_execute_split_fail_fast(self):
        self.call(["one", "two", "three", "four"]).AndRaise(self.TOO_BIG)
        self.call(["one", "two"]).AndReturn(42)
        self.mox.ReplayAll()

        self.assertEqual(42, self.xargs.execute(["one", "two", "three", "four"]))

    def test_execute_split_fail_slow(self):
        self.call(["one", "two", "three", "four"]).AndRaise(self.TOO_BIG)
        self.call(["one", "two"]).AndReturn(0)
        self.call(["three", "four"]).AndReturn(42)
        self.mox.ReplayAll()

        self.assertEqual(42, self.xargs.execute(["one", "two", "three", "four"]))
示例#21
0
 def setUp(self):
     super(XargsTest, self).setUp()
     self.call = self.mox.CreateMockAnything()
     self.xargs = Xargs(self.call)
示例#22
0
class XargsTest(mox.MoxTestBase):
    def setUp(self):
        super(XargsTest, self).setUp()
        self.call = self.mox.CreateMockAnything()
        self.xargs = Xargs(self.call)

    def test_execute_nosplit_success(self):
        self.call(['one', 'two', 'three', 'four']).AndReturn(0)
        self.mox.ReplayAll()

        self.assertEqual(0, self.xargs.execute(['one', 'two', 'three',
                                                'four']))

    def test_execute_nosplit_raise(self):
        exception = Exception()

        self.call(['one', 'two', 'three', 'four']).AndRaise(exception)
        self.mox.ReplayAll()

        with pytest.raises(Exception) as raised:
            self.xargs.execute(['one', 'two', 'three', 'four'])
        self.assertTrue(exception is raised.value)

    def test_execute_nosplit_fail(self):
        self.call(['one', 'two', 'three', 'four']).AndReturn(42)
        self.mox.ReplayAll()

        self.assertEqual(42,
                         self.xargs.execute(['one', 'two', 'three', 'four']))

    TOO_BIG = OSError(errno.E2BIG, os.strerror(errno.E2BIG))

    def test_execute_split(self):
        self.call(['one', 'two', 'three', 'four']).AndRaise(self.TOO_BIG)
        self.call(['one', 'two']).AndReturn(0)
        self.call(['three', 'four']).AndReturn(0)
        self.mox.ReplayAll()

        self.assertEqual(0, self.xargs.execute(['one', 'two', 'three',
                                                'four']))

    def test_execute_uneven(self):
        self.call(['one', 'two', 'three']).AndRaise(self.TOO_BIG)
        # TODO(John Sirois): We really don't care if the 1st call gets 1 argument or 2, we just
        # care that all arguments get passed just once via exactly 2 rounds of call - consider making
        # this test less brittle to changes in the chunking logic.
        self.call(['one']).AndReturn(0)
        self.call(['two', 'three']).AndReturn(0)
        self.mox.ReplayAll()

        self.assertEqual(0, self.xargs.execute(['one', 'two', 'three']))

    def test_execute_split_multirecurse(self):
        self.call(['one', 'two', 'three', 'four']).AndRaise(self.TOO_BIG)
        self.call(['one', 'two']).AndRaise(self.TOO_BIG)
        self.call(['one']).AndReturn(0)
        self.call(['two']).AndReturn(0)
        self.call(['three', 'four']).AndReturn(0)
        self.mox.ReplayAll()

        self.assertEqual(0, self.xargs.execute(['one', 'two', 'three',
                                                'four']))

    def test_execute_split_fail_fast(self):
        self.call(['one', 'two', 'three', 'four']).AndRaise(self.TOO_BIG)
        self.call(['one', 'two']).AndReturn(42)
        self.mox.ReplayAll()

        self.assertEqual(42,
                         self.xargs.execute(['one', 'two', 'three', 'four']))

    def test_execute_split_fail_slow(self):
        self.call(['one', 'two', 'three', 'four']).AndRaise(self.TOO_BIG)
        self.call(['one', 'two']).AndReturn(0)
        self.call(['three', 'four']).AndReturn(42)
        self.mox.ReplayAll()

        self.assertEqual(42,
                         self.xargs.execute(['one', 'two', 'three', 'four']))
示例#23
0
 def setUp(self):
     self.call = unittest.mock.Mock()
     self.xargs = Xargs(self.call)