]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a `puppet-lint` linter.
authorJoshua Spence <josh@joshuaspence.com>
Tue, 6 May 2014 03:42:40 +0000 (20:42 -0700)
committerepriestley <git@epriestley.com>
Tue, 6 May 2014 03:42:40 +0000 (20:42 -0700)
Summary: This linter is a wrapper around [[http://puppet-lint.com/ | puppet-lint[]].

Test Plan: Wrote an executed unit tests.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: epriestley, Korvin

Differential Revision: https://secure.phabricator.com/D8990

17 files changed:
src/__phutil_library_map__.php
src/lint/linter/ArcanistPuppetLintLinter.php [new file with mode: 0644]
src/lint/linter/__tests__/ArcanistPuppetLintLinterTestCase.php [new file with mode: 0644]
src/lint/linter/__tests__/puppet-lint/2sp_soft_tabs.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/puppet-lint/arrow_alignment.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/puppet-lint/double_quoted_strings.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/puppet-lint/duplicate_params.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/puppet-lint/ensure_not_symlink_target.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/puppet-lint/file_mode.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/puppet-lint/hard_tabs.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/puppet-lint/names_containing_dash.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/puppet-lint/quoted_booleans.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/puppet-lint/right_to_left_relationship.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/puppet-lint/site.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/puppet-lint/slash_comments.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/puppet-lint/star_comments.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/puppet-lint/unquoted_file_mode.lint-test [new file with mode: 0644]

index a190698ce7131fb5ff671f8b4ec182febdc467bb..919edc66969236f250460108ca213d4e264bf667 100644 (file)
@@ -125,6 +125,8 @@ phutil_register_library_map(array(
     'ArcanistPhutilTestTerminatedException' => 'unit/engine/phutil/testcase/ArcanistPhutilTestTerminatedException.php',
     'ArcanistPhutilXHPASTLinter' => 'lint/linter/ArcanistPhutilXHPASTLinter.php',
     'ArcanistPhutilXHPASTLinterTestCase' => 'lint/linter/__tests__/ArcanistPhutilXHPASTLinterTestCase.php',
+    'ArcanistPuppetLintLinter' => 'lint/linter/ArcanistPuppetLintLinter.php',
+    'ArcanistPuppetLintLinterTestCase' => 'lint/linter/__tests__/ArcanistPuppetLintLinterTestCase.php',
     'ArcanistPyFlakesLinter' => 'lint/linter/ArcanistPyFlakesLinter.php',
     'ArcanistPyFlakesLinterTestCase' => 'lint/linter/__tests__/ArcanistPyFlakesLinterTestCase.php',
     'ArcanistPyLintLinter' => 'lint/linter/ArcanistPyLintLinter.php',
@@ -281,6 +283,8 @@ phutil_register_library_map(array(
     'ArcanistPhutilTestTerminatedException' => 'Exception',
     'ArcanistPhutilXHPASTLinter' => 'ArcanistBaseXHPASTLinter',
     'ArcanistPhutilXHPASTLinterTestCase' => 'ArcanistArcanistLinterTestCase',
+    'ArcanistPuppetLintLinter' => 'ArcanistExternalLinter',
+    'ArcanistPuppetLintLinterTestCase' => 'ArcanistArcanistLinterTestCase',
     'ArcanistPyFlakesLinter' => 'ArcanistExternalLinter',
     'ArcanistPyFlakesLinterTestCase' => 'ArcanistArcanistLinterTestCase',
     'ArcanistPyLintLinter' => 'ArcanistLinter',
diff --git a/src/lint/linter/ArcanistPuppetLintLinter.php b/src/lint/linter/ArcanistPuppetLintLinter.php
new file mode 100644 (file)
index 0000000..7dab3ed
--- /dev/null
@@ -0,0 +1,92 @@
+<?php
+
+/**
+ * A linter for Puppet files.
+ */
+final class ArcanistPuppetLintLinter extends ArcanistExternalLinter {
+
+  public function getLinterName() {
+    return 'PUPPETLINT';
+  }
+
+  public function getLinterConfigurationName() {
+    return 'puppet-lint';
+  }
+
+  public function getDefaultBinary() {
+    return 'puppet-lint';
+  }
+
+  public function getVersion() {
+    list($stdout) = execx('%C --version', $this->getExecutableCommand());
+
+    $matches = array();
+    $regex = '/^Puppet-lint (?P<version>\d+\.\d+\.\d+)$/';
+    if (preg_match($regex, $stdout, $matches)) {
+      return $matches['version'];
+    } else {
+      return false;
+    }
+  }
+
+  public function getInstallInstructions() {
+    return pht('Install puppet-lint using `gem install puppet-lint`.');
+  }
+
+  public function shouldExpectCommandErrors() {
+    return true;
+  }
+
+  public function supportsReadDataFromStdin() {
+    return false;
+  }
+
+  protected function getMandatoryFlags() {
+    return array(sprintf('--log-format=%s', implode('|', array(
+      '%{linenumber}',
+      '%{column}',
+      '%{kind}',
+      '%{check}',
+      '%{message}'))));
+  }
+
+  protected function parseLinterOutput($path, $err, $stdout, $stderr) {
+    $lines = phutil_split_lines($stdout, false);
+
+    $messages = array();
+    foreach ($lines as $line) {
+      $matches = explode('|', $line, 5);
+
+      if (count($matches) === 5) {
+        $message = new ArcanistLintMessage();
+        $message->setPath($path);
+        $message->setLine($matches[0]);
+        $message->setChar($matches[1]);
+        $message->setName(ucwords(str_replace('_', ' ', $matches[3])));
+        $message->setDescription(ucfirst($matches[4]));
+
+        switch ($matches[2]) {
+          case 'warning':
+            $message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING);
+            break;
+
+          case 'error':
+            $message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR);
+            break;
+
+          default:
+            $message->setSeverity(ArcanistLintSeverity::SEVERITY_ADVICE);
+            break;
+        }
+
+        $messages[] = $message;
+      }
+    }
+
+    if ($err && !$messages) {
+      return false;
+    }
+
+    return $messages;
+  }
+}
diff --git a/src/lint/linter/__tests__/ArcanistPuppetLintLinterTestCase.php b/src/lint/linter/__tests__/ArcanistPuppetLintLinterTestCase.php
new file mode 100644 (file)
index 0000000..2ce0f1f
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+
+final class ArcanistPuppetLintLinterTestCase
+  extends ArcanistArcanistLinterTestCase {
+
+  public function testPuppetLintLinter() {
+    return $this->executeTestsInDirectory(
+      dirname(__FILE__).'/puppet-lint/',
+      new ArcanistPuppetLintLinter());
+  }
+
+}
diff --git a/src/lint/linter/__tests__/puppet-lint/2sp_soft_tabs.lint-test b/src/lint/linter/__tests__/puppet-lint/2sp_soft_tabs.lint-test
new file mode 100644 (file)
index 0000000..10205a1
--- /dev/null
@@ -0,0 +1,7 @@
+exec { 'test':
+   subscribe   => File['/etc/test'],
+   refreshonly => true,
+}
+~~~~~~~~~~
+error:2:1
+error:3:1
diff --git a/src/lint/linter/__tests__/puppet-lint/arrow_alignment.lint-test b/src/lint/linter/__tests__/puppet-lint/arrow_alignment.lint-test
new file mode 100644 (file)
index 0000000..017d6e2
--- /dev/null
@@ -0,0 +1,6 @@
+exec { 'test':
+  subscribe => File['/etc/test'],
+  refreshonly => true,
+}
+~~~~~~~~~~
+warning:3:15
diff --git a/src/lint/linter/__tests__/puppet-lint/double_quoted_strings.lint-test b/src/lint/linter/__tests__/puppet-lint/double_quoted_strings.lint-test
new file mode 100644 (file)
index 0000000..9132fca
--- /dev/null
@@ -0,0 +1,7 @@
+user { 'dave':
+  ensure  => present,
+  home    => '/home/dave',
+  comment => "This is a double quoted string",
+}
+~~~~~~~~~~
+warning:4:14
diff --git a/src/lint/linter/__tests__/puppet-lint/duplicate_params.lint-test b/src/lint/linter/__tests__/puppet-lint/duplicate_params.lint-test
new file mode 100644 (file)
index 0000000..8f9fb4a
--- /dev/null
@@ -0,0 +1,6 @@
+service { 'anacron':
+  ensure => 'absent',
+  ensure => 'stopped',
+}
+~~~~~~~~~~
+error:3:3
diff --git a/src/lint/linter/__tests__/puppet-lint/ensure_not_symlink_target.lint-test b/src/lint/linter/__tests__/puppet-lint/ensure_not_symlink_target.lint-test
new file mode 100644 (file)
index 0000000..431e950
--- /dev/null
@@ -0,0 +1,5 @@
+file { '/var/log/syslog':
+  ensure => '/var/log/messages',
+}
+~~~~~~~~~~
+warning:2:13
diff --git a/src/lint/linter/__tests__/puppet-lint/file_mode.lint-test b/src/lint/linter/__tests__/puppet-lint/file_mode.lint-test
new file mode 100644 (file)
index 0000000..2d98f90
--- /dev/null
@@ -0,0 +1,6 @@
+file { '/var/log/syslog':
+  ensure => present,
+  mode   => '644',
+}
+~~~~~~~~~~
+warning:3:13
diff --git a/src/lint/linter/__tests__/puppet-lint/hard_tabs.lint-test b/src/lint/linter/__tests__/puppet-lint/hard_tabs.lint-test
new file mode 100644 (file)
index 0000000..0e2aaf6
--- /dev/null
@@ -0,0 +1,7 @@
+exec { 'test':
+               subscribe   => File['/etc/test'],
+               refreshonly => true,
+}
+~~~~~~~~~~
+error:2:1
+error:3:1
diff --git a/src/lint/linter/__tests__/puppet-lint/names_containing_dash.lint-test b/src/lint/linter/__tests__/puppet-lint/names_containing_dash.lint-test
new file mode 100644 (file)
index 0000000..8456485
--- /dev/null
@@ -0,0 +1,3 @@
+$foo-bar123
+~~~~~~~~~~
+warning:1:1
diff --git a/src/lint/linter/__tests__/puppet-lint/quoted_booleans.lint-test b/src/lint/linter/__tests__/puppet-lint/quoted_booleans.lint-test
new file mode 100644 (file)
index 0000000..2acfac5
--- /dev/null
@@ -0,0 +1,6 @@
+service { 'anacron':
+  ensure => 'stopped',
+  enable => 'true',
+}
+~~~~~~~~~~
+warning:3:13
diff --git a/src/lint/linter/__tests__/puppet-lint/right_to_left_relationship.lint-test b/src/lint/linter/__tests__/puppet-lint/right_to_left_relationship.lint-test
new file mode 100644 (file)
index 0000000..43a157e
--- /dev/null
@@ -0,0 +1,3 @@
+Service['httpd'] <- Package['httpd']
+~~~~~~~~~~
+warning:1:18
diff --git a/src/lint/linter/__tests__/puppet-lint/site.lint-test b/src/lint/linter/__tests__/puppet-lint/site.lint-test
new file mode 100644 (file)
index 0000000..b9cf386
--- /dev/null
@@ -0,0 +1,19 @@
+import 'classes/*'
+import 'nodes.pp'
+
+Exec['apt-get-update'] -> Package <| |>
+
+Exec {
+  path => '/usr/bin:/usr/sbin:/bin',
+}
+
+exec { 'apt-get-update':
+  command => '/usr/bin/apt-get update',
+  require => File['/etc/apt/apt.conf.d/01proxy'],
+}
+
+file { '/etc/apt/apt.conf.d/01proxy':
+  ensure => 'file',
+  source => '/tmp/vagrant-puppet/manifests/files/apt-conf',
+}
+~~~~~~~~~~
diff --git a/src/lint/linter/__tests__/puppet-lint/slash_comments.lint-test b/src/lint/linter/__tests__/puppet-lint/slash_comments.lint-test
new file mode 100644 (file)
index 0000000..0277049
--- /dev/null
@@ -0,0 +1,3 @@
+// This is a comment
+~~~~~~~~~~
+warning:1:1
diff --git a/src/lint/linter/__tests__/puppet-lint/star_comments.lint-test b/src/lint/linter/__tests__/puppet-lint/star_comments.lint-test
new file mode 100644 (file)
index 0000000..e30b569
--- /dev/null
@@ -0,0 +1,3 @@
+/* This is a comment */
+~~~~~~~~~~
+warning:1:1
diff --git a/src/lint/linter/__tests__/puppet-lint/unquoted_file_mode.lint-test b/src/lint/linter/__tests__/puppet-lint/unquoted_file_mode.lint-test
new file mode 100644 (file)
index 0000000..e22e2e5
--- /dev/null
@@ -0,0 +1,6 @@
+file { '/var/log/syslog':
+  ensure => present,
+  mode   => 0644,
+}
+~~~~~~~~~~
+warning:3:13