Exemplo n.º 1
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)
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
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)
Exemplo n.º 6
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 = 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)
Exemplo n.º 7
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)
Exemplo n.º 8
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"]))
Exemplo n.º 9
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']))
Exemplo n.º 10
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)
Exemplo n.º 11
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)