diff --git a/lib/rscons.rb b/lib/rscons.rb index 067bd23..bf8a58a 100644 --- a/lib/rscons.rb +++ b/lib/rscons.rb @@ -66,19 +66,6 @@ module Rscons application.vars(*args) end - # Return whether the given path is an absolute filesystem path. - # - # @param path [String] the path to examine. - # - # @return [Boolean] Whether the given path is an absolute filesystem path. - def absolute_path?(path) - if RUBY_PLATFORM =~ /mingw/ - path =~ %r{^(?:\w:)?[\\/]} - else - path.start_with?("/") - end - end - # Return whether the given target is a phony target. # # @param target [Symbol, String] Target name. diff --git a/lib/rscons/util.rb b/lib/rscons/util.rb index f8f92cf..a5c4762 100644 --- a/lib/rscons/util.rb +++ b/lib/rscons/util.rb @@ -2,6 +2,19 @@ module Rscons module Util class << self + # Return whether the given path is an absolute filesystem path. + # + # @param path [String] the path to examine. + # + # @return [Boolean] Whether the given path is an absolute filesystem path. + def absolute_path?(path) + if RUBY_PLATFORM =~ /mingw/ + path =~ %r{^(?:\w:)?[\\/]} + else + path.start_with?("/") + end + end + # Make a relative path corresponding to a possibly absolute one. # # @param path [String] @@ -10,7 +23,7 @@ module Rscons # @return [String] # Relative path. def make_relative_path(path) - if Rscons.absolute_path?(path) + if absolute_path?(path) if path =~ %r{^(\w):(.*)$} "_#{$1}#{$2}" else diff --git a/spec/rscons/util_spec.rb b/spec/rscons/util_spec.rb index dc1190b..04495a0 100644 --- a/spec/rscons/util_spec.rb +++ b/spec/rscons/util_spec.rb @@ -1,6 +1,34 @@ module Rscons describe Util do + describe ".absolute_path?" do + context "on Windows" do + it "returns whether a path is absolute" do + stub_const("RUBY_PLATFORM", "mingw") + expect(Util.absolute_path?("/foo")).to be_truthy + expect(Util.absolute_path?("\\Windows")).to be_truthy + expect(Util.absolute_path?("C:\\Windows")).to be_truthy + expect(Util.absolute_path?("f:\\stuff")).to be_truthy + expect(Util.absolute_path?("g:/projects")).to be_truthy + expect(Util.absolute_path?("x:foo")).to be_falsey + expect(Util.absolute_path?("file.txt")).to be_falsey + end + end + + context "on non-Windows" do + it "returns whether a path is absolute" do + stub_const("RUBY_PLATFORM", "linux") + expect(Util.absolute_path?("/foo")).to be_truthy + expect(Util.absolute_path?("\\Windows")).to be_falsey + expect(Util.absolute_path?("C:\\Windows")).to be_falsey + expect(Util.absolute_path?("f:\\stuff")).to be_falsey + expect(Util.absolute_path?("g:/projects")).to be_falsey + expect(Util.absolute_path?("x:foo")).to be_falsey + expect(Util.absolute_path?("file.txt")).to be_falsey + end + end + end + describe ".make_relative_path" do context "when passed a relative path" do it "returns the path itself" do @@ -10,7 +38,7 @@ module Rscons context "when passed an absolute path" do before(:each) do - expect(Rscons).to receive(:absolute_path?).and_return(true) + expect(Util).to receive(:absolute_path?).and_return(true) end context "on Windows" do @@ -19,7 +47,7 @@ module Rscons end end - context "on POSIX" do + context "on non-Windows" do it "returns a relative path corresponding to an absolute one" do expect(Util.make_relative_path("/foo/bar")).to eq "_/foo/bar" end diff --git a/spec/rscons_spec.rb b/spec/rscons_spec.rb index 7627237..6e45ad7 100644 --- a/spec/rscons_spec.rb +++ b/spec/rscons_spec.rb @@ -1,31 +1,4 @@ describe Rscons do - describe ".absolute_path?" do - context "on Windows" do - it "returns whether a path is absolute" do - stub_const("RUBY_PLATFORM", "mingw") - expect(Rscons.absolute_path?("/foo")).to be_truthy - expect(Rscons.absolute_path?("\\Windows")).to be_truthy - expect(Rscons.absolute_path?("C:\\Windows")).to be_truthy - expect(Rscons.absolute_path?("f:\\stuff")).to be_truthy - expect(Rscons.absolute_path?("g:/projects")).to be_truthy - expect(Rscons.absolute_path?("x:foo")).to be_falsey - expect(Rscons.absolute_path?("file.txt")).to be_falsey - end - end - - context "not on Windows" do - it "returns whether a path is absolute" do - stub_const("RUBY_PLATFORM", "linux") - expect(Rscons.absolute_path?("/foo")).to be_truthy - expect(Rscons.absolute_path?("\\Windows")).to be_falsey - expect(Rscons.absolute_path?("C:\\Windows")).to be_falsey - expect(Rscons.absolute_path?("f:\\stuff")).to be_falsey - expect(Rscons.absolute_path?("g:/projects")).to be_falsey - expect(Rscons.absolute_path?("x:foo")).to be_falsey - expect(Rscons.absolute_path?("file.txt")).to be_falsey - end - end - end describe ".set_suffix" do it "changes the suffix to the new one" do @@ -125,4 +98,5 @@ describe Rscons do end end end + end