summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Dituri <csdituri@gmail.com>2016-11-11 19:05:20 -0600
committerChris Dituri <csdituri@gmail.com>2016-11-11 19:10:06 -0600
commitecd324781e4e43842800eac5a9eda3b9920e130d (patch)
treef795a936defdcc72362eef43df184176e537c5c2
parentc34212ea73a07260ab1e23370276124bae36371b (diff)
downloadohai-ecd324781e4e43842800eac5a9eda3b9920e130d.tar.gz
Added haskell language plugin for ohai
Detects whether ghc, ghci, cabal, or stack are installed Signed-off-by: Chris Dituri <csdituri@gmail.com>
-rw-r--r--lib/ohai/plugins/haskell.rb93
-rw-r--r--spec/unit/plugins/haskell_spec.rb211
2 files changed, 304 insertions, 0 deletions
diff --git a/lib/ohai/plugins/haskell.rb b/lib/ohai/plugins/haskell.rb
new file mode 100644
index 00000000..b3fa2335
--- /dev/null
+++ b/lib/ohai/plugins/haskell.rb
@@ -0,0 +1,93 @@
+# Author:: Chris Dituri (<csdituri@gmail.com>)
+# Copyright:: Copyright (c) 2016 Chris Dituri
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+Ohai.plugin(:Haskell) do
+
+ provides "languages/haskell",
+ "languages/haskell/ghc",
+ "languages/haskell/ghci",
+ "languages/haskell/cabal",
+ "languages/haskell/stack"
+
+ depends "languages"
+
+ collect_data(:default) do
+ haskell = Mash.new
+
+ # Check for ghc
+ begin
+ so = shell_out("ghc --version")
+
+ # Sample output:
+ # The Glorious Glasgow Haskell Compilation System, version 7.6.3
+ if so.exitstatus == 0
+ haskell[:ghc] = Mash.new
+ haskell[:ghc][:version] = so.stdout.split[-1]
+ haskell[:ghc][:description] = so.stdout.chomp
+ end
+ rescue Ohai::Exceptions::Exec
+ Ohai::Log.debug('Haskell plugin: Could not shell_out "ghc --version". Skipping data')
+ end
+
+ # Check for ghci
+ begin
+ so = shell_out("ghci --version")
+
+ # Sample output:
+ # The Glorious Glasgow Haskell Compilation System, version 7.6.3
+ if so.exitstatus == 0
+ haskell[:ghci] = Mash.new
+ haskell[:ghci][:version] = so.stdout.split[-1]
+ haskell[:ghci][:description] = so.stdout.chomp
+ end
+ rescue Ohai::Exceptions::Exec
+ Ohai::Log.debug('Haskell plugin: Could not shell_out "ghci --version". Skipping data')
+ end
+
+ # Check for cabal
+ begin
+ so = shell_out("cabal --version")
+
+ # Sample output:
+ # cabal-install version 1.16.0.2
+ # using version 1.16.0 of the Cabal library
+ if so.exitstatus == 0
+ haskell[:cabal] = Mash.new
+ haskell[:cabal][:version] = so.stdout.split("\n")[0].split[-1]
+ haskell[:cabal][:description] = so.stdout.split("\n")[0].chomp
+ end
+ rescue Ohai::Exceptions::Exec
+ Ohai::Log.debug('Haskell plugin: Could not shell_out "cabal --version". Skipping data')
+ end
+
+ # Check for stack
+ begin
+ so = shell_out("stack --version")
+
+ # Sample output:
+ # Version 1.1.0, Git revision 0e9430aad55841b5ff2c6c2851f0548c16bce7cf (3540 commits) x86_64 hpack-0.13.0
+ if so.exitstatus == 0
+ haskell[:stack] = Mash.new
+ haskell[:stack][:version] = so.stdout.split[1][0..-2]
+ haskell[:stack][:description] = so.stdout.chomp
+ end
+ rescue Ohai::Exceptions::Exec
+ Ohai::Log.debug('Haskell plugin: Could not shell_out "stack --version". Skipping data')
+ end
+
+ languages[:haskell] = haskell unless haskell.empty?
+ end
+end
diff --git a/spec/unit/plugins/haskell_spec.rb b/spec/unit/plugins/haskell_spec.rb
new file mode 100644
index 00000000..05e632c2
--- /dev/null
+++ b/spec/unit/plugins/haskell_spec.rb
@@ -0,0 +1,211 @@
+# Author:: Chris Dituri (<csdituri@gmail.com>)
+# Copyright:: Copyright (c) 2016 Chris Dituri
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "/spec_helper.rb"))
+
+describe Ohai::System, "plugin haskell" do
+
+ let(:plugin) do
+ plugin = get_plugin("haskell").tap do |plugin|
+ plugin[:languages] = Mash.new
+ end
+ end
+
+ let(:ghc_out) { "The Glorious Glasgow Haskell Compilation System, version 7.6.3" }
+ let(:ghci_out) { "The Glorious Glasgow Haskell Compilation System, version 7.6.3" }
+ let(:cabal_out) { "cabal-install version 1.16.0.2\nusing version 1.16.0 of the Cabal library" }
+ let(:stack_out) { "Version 1.1.0, Git revision 0e9430aad55841b5ff2c6c2851f0548c16bce7cf (3540 commits) x86_64 hpack-0.13.0" }
+
+ def setup_plugin
+ allow(plugin).to receive(:shell_out)
+ .with("ghc --version")
+ .and_return(mock_shell_out(0, ghc_out, ""))
+ allow(plugin).to receive(:shell_out)
+ .with("ghci --version")
+ .and_return(mock_shell_out(0, ghci_out, ""))
+ allow(plugin).to receive(:shell_out)
+ .with("cabal --version")
+ .and_return(mock_shell_out(0, cabal_out, ""))
+ allow(plugin).to receive(:shell_out)
+ .with("stack --version")
+ .and_return(mock_shell_out(0, stack_out, ""))
+ end
+
+ context "if haskell/ghc is installed" do
+
+ before(:each) do
+ setup_plugin
+ plugin.run
+ end
+
+ it "set languages[:haskell][:ghc][:version]" do
+ expect(plugin[:languages][:haskell][:ghc][:version]).to eql("7.6.3")
+ end
+
+ it "set languages[:haskell][:ghc][:description]" do
+ expect(plugin[:languages][:haskell][:ghc][:description]).to eql(ghc_out)
+ end
+ end
+
+ context "if haskell/ghci is installed" do
+
+ before(:each) do
+ setup_plugin
+ plugin.run
+ end
+
+ it "set languages[:haskell][:ghci][:version]" do
+ expect(plugin[:languages][:haskell][:ghci][:version]).to eql("7.6.3")
+ end
+
+ it "set languages[:haskell][:ghci][:description]" do
+ expect(plugin[:languages][:haskell][:ghci][:description]).to eql(ghci_out)
+ end
+ end
+
+ context "if haskell/cabal is installed" do
+
+ before(:each) do
+ setup_plugin
+ plugin.run
+ end
+
+ it "set languages[:haskell][:cabal][:version]" do
+ expect(plugin[:languages][:haskell][:cabal][:version]).to eql("1.16.0.2")
+ end
+
+ it "set languages[:haskell][:cabal][:description]" do
+ expect(plugin[:languages][:haskell][:cabal][:description]).to eql(cabal_out.split("\n")[0].chomp)
+ end
+ end
+
+ context "if haskell/stack is installed" do
+
+ before(:each) do
+ setup_plugin
+ plugin.run
+ end
+
+ it "set languages[:haskell][:stack][:version]" do
+ expect(plugin[:languages][:haskell][:stack][:version]).to eql("1.1.0")
+ end
+
+ it "set languages[:haskell][:stack][:description]" do
+ expect(plugin[:languages][:haskell][:stack][:description]).to eql(stack_out)
+ end
+ end
+
+ context "if haskell is NOT installed" do
+
+ before(:each) do
+ allow(plugin).to receive(:shell_out)
+ .and_raise( Ohai::Exceptions::Exec )
+ plugin.run
+ end
+
+ it "do NOT set languages[:haskell]" do
+ expect(plugin[:languages]).not_to have_key(:haskell)
+ end
+ end
+
+ context "if haskell/ghc is NOT installed" do
+ before(:each) do
+ allow(plugin).to receive(:shell_out)
+ .with("ghc --version")
+ .and_raise( Ohai::Exceptions::Exec )
+ allow(plugin).to receive(:shell_out)
+ .with("ghci --version")
+ .and_return(mock_shell_out(0, ghci_out, ""))
+ allow(plugin).to receive(:shell_out)
+ .with("cabal --version")
+ .and_return(mock_shell_out(0, cabal_out, ""))
+ allow(plugin).to receive(:shell_out)
+ .with("stack --version")
+ .and_return(mock_shell_out(0, stack_out, ""))
+ plugin.run
+ end
+
+ it "do NOT set languages[:haskell][:ghc]" do
+ expect(plugin[:languages][:haskell]).not_to have_key(:ghc)
+ end
+ end
+
+ context "if haskell/ghci is NOT installed" do
+ before(:each) do
+ allow(plugin).to receive(:shell_out)
+ .with("ghc --version")
+ .and_return(mock_shell_out(0, ghc_out, ""))
+ allow(plugin).to receive(:shell_out)
+ .with("ghci --version")
+ .and_raise( Ohai::Exceptions::Exec )
+ allow(plugin).to receive(:shell_out)
+ .with("cabal --version")
+ .and_return(mock_shell_out(0, cabal_out, ""))
+ allow(plugin).to receive(:shell_out)
+ .with("stack --version")
+ .and_return(mock_shell_out(0, stack_out, ""))
+ plugin.run
+ end
+
+ it "do NOT set languages[:haskell][:ghci]" do
+ expect(plugin[:languages][:haskell]).not_to have_key(:ghci)
+ end
+ end
+
+ context "if haskell/cabal is NOT installed" do
+ before(:each) do
+ allow(plugin).to receive(:shell_out)
+ .with("ghc --version")
+ .and_return(mock_shell_out(0, ghc_out, ""))
+ allow(plugin).to receive(:shell_out)
+ .with("ghci --version")
+ .and_return(mock_shell_out(0, ghci_out, ""))
+ allow(plugin).to receive(:shell_out)
+ .with("cabal --version")
+ .and_raise( Ohai::Exceptions::Exec )
+ allow(plugin).to receive(:shell_out)
+ .with("stack --version")
+ .and_return(mock_shell_out(0, stack_out, ""))
+ plugin.run
+ end
+
+ it "do NOT set languages[:haskell][:cabal]" do
+ expect(plugin[:languages][:haskell]).not_to have_key(:cabal)
+ end
+ end
+
+ context "if haskell/stack is NOT installed" do
+ before(:each) do
+ allow(plugin).to receive(:shell_out)
+ .with("ghc --version")
+ .and_return(mock_shell_out(0, ghc_out, ""))
+ allow(plugin).to receive(:shell_out)
+ .with("ghci --version")
+ .and_return(mock_shell_out(0, ghci_out, ""))
+ allow(plugin).to receive(:shell_out)
+ .with("cabal --version")
+ .and_return(mock_shell_out(0, cabal_out, ""))
+ allow(plugin).to receive(:shell_out)
+ .with("stack --version")
+ .and_raise( Ohai::Exceptions::Exec )
+ plugin.run
+ end
+
+ it "do NOT set languages[:haskell][:stack]" do
+ expect(plugin[:languages][:haskell]).not_to have_key(:stack)
+ end
+ end
+end