[Pkg-puppet-devel] [facter] 47/61: (#21738) Add .cmd / Update PowerShell parser

Stig Sandbeck Mathisen ssm at debian.org
Mon Nov 4 15:02:03 UTC 2013


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit 6423c0d7d56b80d0c0b88636470cf896d01b7f2b
Author: Rob Reynolds <ferventcoder at gmail.com>
Date:   Tue Jul 30 12:39:01 2013 -0500

    (#21738) Add .cmd / Update PowerShell parser
    
    This updates the PowerShell parser to add a switch for the Bypass execution
    policy which means that the execution policy will not need to be set
    prior to running facter. This also adds some switches (NoProfile,
    NoLogo, NonInteractive) that speed up the execution of external powershell
    facts.
    
    This also adds the extension .cmd as a valid file extension as part of the
    ScriptParser. This functions in the same way as .bat.
---
 lib/facter/util/parser.rb     |   17 +++++------------
 spec/unit/util/parser_spec.rb |   35 ++++++++++++++++++++++++++++++++---
 2 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/lib/facter/util/parser.rb b/lib/facter/util/parser.rb
index d03bc4f..36f1358 100644
--- a/lib/facter/util/parser.rb
+++ b/lib/facter/util/parser.rb
@@ -126,24 +126,17 @@ module Facter::Util::Parser
 
   register(ScriptParser) do |filename|
     if Facter::Util::Config.is_windows?
-      extension_matches?(filename, %w{bat com exe})
+      extension_matches?(filename, %w{bat cmd com exe}) && File.file?(filename)
     else
-      File.executable?(filename)
+      File.executable?(filename) && File.file?(filename)
     end
   end
 
   # Executes and parses the key value output of Powershell scripts
-  #
-  # Before you can run unsigned ps1 scripts it requires a change to execution
-  # policy:
-  #
-  #   Set-ExecutionPolicy RemoteSigned -Scope LocalMachine
-  #   Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
-  #
   class PowershellParser < Base
     # Returns a hash of facts from powershell output
     def results
-      shell_command = "powershell -File #{filename}"
+      shell_command = "powershell -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -File \"#{filename}\""
       output = Facter::Util::Resolution.exec(shell_command)
 
       result = {}
@@ -161,9 +154,9 @@ module Facter::Util::Parser
   end
 
   register(PowershellParser) do |filename|
-    Facter::Util::Config.is_windows? && extension_matches?(filename, "ps1")
+    Facter::Util::Config.is_windows? && extension_matches?(filename, "ps1") && File.file?(filename)
   end
-  
+
   # A parser that is used when there is no other parser that can handle the file
   # The return from results indicates to the caller the file was not parsed correctly.
   class NothingParser
diff --git a/spec/unit/util/parser_spec.rb b/spec/unit/util/parser_spec.rb
index be7b9e7..00bba5d 100644
--- a/spec/unit/util/parser_spec.rb
+++ b/spec/unit/util/parser_spec.rb
@@ -1,7 +1,6 @@
 #!/usr/bin/env ruby
 
 require 'spec_helper'
-
 require 'facter/util/parser'
 require 'tempfile'
 require 'tmpdir.rb'
@@ -121,12 +120,42 @@ describe Facter::Util::Parser do
         Facter::Util::Parser.parser_for(cmd)
       end
 
-      it "should return no results" do
-        parser.results.should be_nil
+      it "should not parse a directory" do
+        File.stubs(:file?).with(cmd).returns(false)
+        Facter::Util::Parser.parser_for(cmd).results.should be_nil
+      end
+
+      it "should return the data properly" do
+        File.stubs(:file?).with(cmd).returns(true)
+        parser.results.should == data
       end
     end
   end
 
+  describe "powershell parser" do
+    let :ps1 do "/tmp/foo.ps1" end
+    let :data_in_ps1 do "one=two\nthree=four\n" end
+
+    before :each do
+      Facter::Util::Config.stubs(:is_windows?).returns(true)
+      Facter::Util::Resolution.stubs(:exec).returns(data_in_ps1)
+    end
+
+    let :parser do
+      Facter::Util::Parser.parser_for(ps1)
+    end
+
+    it "should not parse a directory" do
+      File.stubs(:file?).with(ps1).returns(false)
+      Facter::Util::Parser.parser_for(ps1).results.should be_nil
+    end
+
+    it "should return data properly" do
+      File.stubs(:file?).with(ps1).returns(true)
+      parser.results.should == data
+    end
+  end
+
   describe "nothing parser" do
     it "uses the nothing parser when there is no other parser" do
       Facter::Util::Parser.parser_for("this.is.not.valid").results.should be_nil

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git



More information about the Pkg-puppet-devel mailing list