diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2017-02-07 13:19:08 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2017-02-07 13:19:08 -0800 |
commit | 2f67ecb8e69ac10956145c64c612537b82ddd971 (patch) | |
tree | a3975cb3ac5c8b300ad26dceccc9464bfbda6f1f /spec | |
parent | ac7cda9de8a41754226894336794b7972d33a1b8 (diff) | |
download | chef-2f67ecb8e69ac10956145c64c612537b82ddd971.tar.gz |
add tests for 'where'
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'spec')
-rw-r--r-- | spec/unit/mixin/which.rb | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/spec/unit/mixin/which.rb b/spec/unit/mixin/which.rb index 50179a9b11..179251f86c 100644 --- a/spec/unit/mixin/which.rb +++ b/spec/unit/mixin/which.rb @@ -1,6 +1,6 @@ # # Author:: Seth Chisamore (<schisamo@chef.io>) -# Copyright:: Copyright 2011-2016, Chef Software Inc. +# Copyright:: Copyright 2011-2017, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -101,6 +101,48 @@ describe Chef::Mixin::Which do end end + def self.test_where(description, *args, finds: [], &block) + it description do + # stub the ENV['PATH'] + expect(test).to receive(:env_path).and_return(["/dir1", "/dir2" ].join(File::PATH_SEPARATOR)) + + # most files should not be found + allow(File).to receive(:executable?).and_return(false) + allow(File).to receive(:directory?).and_return(false) + + # stub the expectation + finds.each do |path| + expect(File).to receive(:executable?).with(path).and_return(true) + end + + # setup the actual expectation on the return value + expect(test.where(*args, &block)).to eql(finds) + end + end + context "where" do + context "simple usage" do + test_where("returns empty array when it doesn't find anything", "foo1") + + ["/dir1", "/dir2", "/bin", "/usr/bin", "/sbin", "/usr/sbin" ].each do |dir| + test_where("finds `foo1` in #{dir} when it is stubbed", "foo1", finds: [ "#{dir}/foo1" ]) + end + + test_where("finds `foo1` in all directories", "foo1", finds: [ "/dir1/foo1", "/dir2/foo1" ]) + end + + context "with an array of args" do + test_where("finds the first arg", "foo1", "foo2", finds: [ "/dir2/foo1" ]) + + test_where("finds the second arg", "foo1", "foo2", finds: [ "/dir2/foo2" ]) + + test_where("finds foo1 before foo2", "foo1", "foo2", finds: [ "/dir2/foo1", "/dir1/foo2" ]) + + test_where("finds foo1 before foo2 if the dirs are reversed", "foo1", "foo2", finds: [ "/dir1/foo1", "/dir2/foo2" ]) + + test_where("finds them both in the same directory", "foo1", "foo2", finds: [ "/dir1/foo1", "/dir1/foo2" ]) + + test_where("finds foo2 first if they're reversed", "foo2", "foo1", finds: [ "/dir1/foo2", "/dir1/foo1" ]) + end end end |