summaryrefslogtreecommitdiff
path: root/spec/unit/util
diff options
context:
space:
mode:
authorJohn Keiser <jkeiser@opscode.com>2014-08-27 21:06:59 -0700
committerAdam Edwards <adamed@opscode.com>2014-08-30 12:31:09 -0700
commitbc3d6871086721d2049bd2e28e8801d0eed176ff (patch)
tree48b608e6bd893e3697350433c7efd1aacb9cfca8 /spec/unit/util
parent482f8bb073ddd322017b9366fb8aac2301e56e3a (diff)
downloadchef-bc3d6871086721d2049bd2e28e8801d0eed176ff.tar.gz
Move Chef::Config path functions to Chef::Util::PathHelper
Diffstat (limited to 'spec/unit/util')
-rw-r--r--spec/unit/util/path_helper_spec.rb134
1 files changed, 106 insertions, 28 deletions
diff --git a/spec/unit/util/path_helper_spec.rb b/spec/unit/util/path_helper_spec.rb
index 36ba19fce6..eb89aa083a 100644
--- a/spec/unit/util/path_helper_spec.rb
+++ b/spec/unit/util/path_helper_spec.rb
@@ -20,101 +20,179 @@ require 'chef/util/path_helper'
require 'spec_helper'
describe Chef::Util::PathHelper do
- let(:path_helper) { Chef::Util::PathHelper }
+ PathHelper = Chef::Util::PathHelper
+
+ [ false, true ].each do |is_windows|
+ context "on #{is_windows ? "windows" : "unix"}" do
+ before(:each) do
+ Chef::Platform.stub(:windows?).and_return(is_windows)
+ end
+
+ describe "join" do
+ it "joins components when some end with separators" do
+ expected = PathHelper.cleanpath("/foo/bar/baz")
+ expected = "C:#{expected}" if is_windows
+ PathHelper.join(is_windows ? 'C:\\foo\\' : "/foo/", "bar", "baz").should == expected
+ end
+
+ it "joins components that don't end in separators" do
+ expected = PathHelper.cleanpath("/foo/bar/baz")
+ expected = "C:#{expected}" if is_windows
+ PathHelper.join(is_windows ? 'C:\\foo' : "/foo", "bar", "baz").should == expected
+ end
+
+ it "joins starting with '' resolve to absolute paths" do
+ PathHelper.join('', 'a', 'b').should == "#{PathHelper.path_separator}a#{PathHelper.path_separator}b"
+ end
+
+ it "joins ending with '' add a / to the end" do
+ PathHelper.join('a', 'b', '').should == "a#{PathHelper.path_separator}b#{PathHelper.path_separator}"
+ end
+
+ if is_windows
+ it "joins components on Windows when some end with unix separators" do
+ expected = 'C:\\foo/bar\\baz'
+ PathHelper.join('C:\\foo/', "bar", "baz").should == expected
+ end
+ end
+ end
+
+ if is_windows
+ it "path_separator is \\" do
+ PathHelper.path_separator.should == '\\'
+ end
+ else
+ it "path_separator is /" do
+ PathHelper.path_separator.should == '/'
+ end
+ end
+
+ if is_windows
+ it "cleanpath changes slashes into backslashes and leaves backslashes alone" do
+ PathHelper.cleanpath('/a/b\\c/d/').should == '\\a\\b\\c\\d'
+ end
+ it "cleanpath does not remove leading double backslash" do
+ PathHelper.cleanpath('\\\\a/b\\c/d/').should == '\\\\a\\b\\c\\d'
+ end
+ else
+ it "cleanpath removes extra slashes alone" do
+ PathHelper.cleanpath('//a///b/c/d/').should == '/a/b/c/d'
+ end
+ end
+
+ describe "dirname" do
+ it "dirname('abc') is '.'" do
+ PathHelper.dirname('abc').should == '.'
+ end
+ it "dirname('/') is '/'" do
+ PathHelper.dirname(PathHelper.path_separator).should == PathHelper.path_separator
+ end
+ it "dirname('a/b/c') is 'a/b'" do
+ PathHelper.dirname(PathHelper.join('a', 'b', 'c')).should == PathHelper.join('a', 'b')
+ end
+ it "dirname('a/b/c/') is 'a/b'" do
+ PathHelper.dirname(PathHelper.join('a', 'b', 'c', '')).should == PathHelper.join('a', 'b')
+ end
+ it "dirname('/a/b/c') is '/a/b'" do
+ PathHelper.dirname(PathHelper.join('', 'a', 'b', 'c')).should == PathHelper.join('', 'a', 'b')
+ end
+ end
+ end
+ end
describe "validate_path" do
context "on windows" do
before(:each) do
# pass by default
Chef::Platform.stub(:windows?).and_return(true)
- path_helper.stub(:printable?).and_return(true)
- path_helper.stub(:windows_max_length_exceeded?).and_return(false)
+ PathHelper.stub(:printable?).and_return(true)
+ PathHelper.stub(:windows_max_length_exceeded?).and_return(false)
end
it "returns the path if the path passes the tests" do
- expect(path_helper.validate_path("C:\\ThisIsRigged")).to eql("C:\\ThisIsRigged")
+ expect(PathHelper.validate_path("C:\\ThisIsRigged")).to eql("C:\\ThisIsRigged")
end
it "does not raise an error if everything looks great" do
- expect { path_helper.validate_path("C:\\cool path\\dude.exe") }.not_to raise_error
+ expect { PathHelper.validate_path("C:\\cool path\\dude.exe") }.not_to raise_error
end
it "raises an error if the path has invalid characters" do
- path_helper.stub(:printable?).and_return(false)
- expect { path_helper.validate_path("Newline!\n") }.to raise_error(Chef::Exceptions::ValidationFailed)
+ PathHelper.stub(:printable?).and_return(false)
+ expect { PathHelper.validate_path("Newline!\n") }.to raise_error(Chef::Exceptions::ValidationFailed)
end
it "Adds the \\\\?\\ prefix if the path exceeds MAX_LENGTH and does not have it" do
long_path = "C:\\" + "a" * 250 + "\\" + "b" * 250
prefixed_long_path = "\\\\?\\" + long_path
- path_helper.stub(:windows_max_length_exceeded?).and_return(true)
- expect(path_helper.validate_path(long_path)).to eql(prefixed_long_path)
+ PathHelper.stub(:windows_max_length_exceeded?).and_return(true)
+ expect(PathHelper.validate_path(long_path)).to eql(prefixed_long_path)
end
end
end
describe "windows_max_length_exceeded?" do
it "returns true if the path is too long (259 + NUL) for the API" do
- expect(path_helper.windows_max_length_exceeded?("C:\\" + "a" * 250 + "\\" + "b" * 6)).to be_true
+ expect(PathHelper.windows_max_length_exceeded?("C:\\" + "a" * 250 + "\\" + "b" * 6)).to be_true
end
it "returns false if the path is not too long (259 + NUL) for the standard API" do
- expect(path_helper.windows_max_length_exceeded?("C:\\" + "a" * 250 + "\\" + "b" * 5)).to be_false
+ expect(PathHelper.windows_max_length_exceeded?("C:\\" + "a" * 250 + "\\" + "b" * 5)).to be_false
end
it "returns false if the path is over 259 characters but uses the \\\\?\\ prefix" do
- expect(path_helper.windows_max_length_exceeded?("\\\\?\\C:\\" + "a" * 250 + "\\" + "b" * 250)).to be_false
+ expect(PathHelper.windows_max_length_exceeded?("\\\\?\\C:\\" + "a" * 250 + "\\" + "b" * 250)).to be_false
end
end
describe "printable?" do
it "returns true if the string contains no non-printable characters" do
- expect(path_helper.printable?("C:\\Program Files (x86)\\Microsoft Office\\Files.lst")).to be_true
+ expect(PathHelper.printable?("C:\\Program Files (x86)\\Microsoft Office\\Files.lst")).to be_true
end
it "returns true when given 'abc' in unicode" do
- expect(path_helper.printable?("\u0061\u0062\u0063")).to be_true
+ expect(PathHelper.printable?("\u0061\u0062\u0063")).to be_true
end
it "returns true when given japanese unicode" do
- expect(path_helper.printable?("\uff86\uff87\uff88")).to be_true
+ expect(PathHelper.printable?("\uff86\uff87\uff88")).to be_true
end
it "returns false if the string contains a non-printable character" do
- expect(path_helper.printable?("\my files\work\notes.txt")).to be_false
+ expect(PathHelper.printable?("\my files\work\notes.txt")).to be_false
end
# This isn't necessarily a requirement, but here to be explicit about functionality.
it "returns false if the string contains a newline or tab" do
- expect(path_helper.printable?("\tThere's no way,\n\t *no* way,\n\t that you came from my loins.\n")).to be_false
+ expect(PathHelper.printable?("\tThere's no way,\n\t *no* way,\n\t that you came from my loins.\n")).to be_false
end
end
describe "canonical_path" do
context "on windows", :windows_only do
it "returns an absolute path with backslashes instead of slashes" do
- expect(path_helper.canonical_path("\\\\?\\C:/windows/win.ini")).to eq("\\\\?\\c:\\windows\\win.ini")
+ expect(PathHelper.canonical_path("\\\\?\\C:/windows/win.ini")).to eq("\\\\?\\c:\\windows\\win.ini")
end
it "adds the \\\\?\\ prefix if it is missing" do
- expect(path_helper.canonical_path("C:/windows/win.ini")).to eq("\\\\?\\c:\\windows\\win.ini")
+ expect(PathHelper.canonical_path("C:/windows/win.ini")).to eq("\\\\?\\c:\\windows\\win.ini")
end
it "returns a lowercase path" do
- expect(path_helper.canonical_path("\\\\?\\C:\\CASE\\INSENSITIVE")).to eq("\\\\?\\c:\\case\\insensitive")
+ expect(PathHelper.canonical_path("\\\\?\\C:\\CASE\\INSENSITIVE")).to eq("\\\\?\\c:\\case\\insensitive")
end
end
context "not on windows", :unix_only do
context "ruby is at least 1.9", :ruby_gte_19_only do
it "returns a canonical path" do
- expect(path_helper.canonical_path("/etc//apache.d/sites-enabled/../sites-available/default")).to eq("/etc/apache.d/sites-available/default")
+ expect(PathHelper.canonical_path("/etc//apache.d/sites-enabled/../sites-available/default")).to eq("/etc/apache.d/sites-available/default")
end
end
context "ruby is less than 1.9", :ruby_18_only do
it "returns a canonical path" do
- expect { path_helper.canonical_path("/etc//apache.d/sites-enabled/../sites-available/default") }.to raise_error(NotImplementedError)
+ expect { PathHelper.canonical_path("/etc//apache.d/sites-enabled/../sites-available/default") }.to raise_error(NotImplementedError)
end
end
end
@@ -122,15 +200,15 @@ describe Chef::Util::PathHelper do
describe "paths_eql?" do
it "returns true if the paths are the same" do
- path_helper.stub(:canonical_path).with("bandit").and_return("c:/bandit/bandit")
- path_helper.stub(:canonical_path).with("../bandit/bandit").and_return("c:/bandit/bandit")
- expect(path_helper.paths_eql?("bandit", "../bandit/bandit")).to be_true
+ PathHelper.stub(:canonical_path).with("bandit").and_return("c:/bandit/bandit")
+ PathHelper.stub(:canonical_path).with("../bandit/bandit").and_return("c:/bandit/bandit")
+ expect(PathHelper.paths_eql?("bandit", "../bandit/bandit")).to be_true
end
it "returns false if the paths are different" do
- path_helper.stub(:canonical_path).with("bandit").and_return("c:/Bo/Bandit")
- path_helper.stub(:canonical_path).with("../bandit/bandit").and_return("c:/bandit/bandit")
- expect(path_helper.paths_eql?("bandit", "../bandit/bandit")).to be_false
+ PathHelper.stub(:canonical_path).with("bandit").and_return("c:/Bo/Bandit")
+ PathHelper.stub(:canonical_path).with("../bandit/bandit").and_return("c:/bandit/bandit")
+ expect(PathHelper.paths_eql?("bandit", "../bandit/bandit")).to be_false
end
end
end