move Rscons.absolute_path?() to Util

This commit is contained in:
Josh Holtrop 2018-11-25 21:31:48 -05:00
parent cd2696dd0b
commit dff80587ac
4 changed files with 45 additions and 43 deletions

View File

@ -66,19 +66,6 @@ module Rscons
application.vars(*args) application.vars(*args)
end 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. # Return whether the given target is a phony target.
# #
# @param target [Symbol, String] Target name. # @param target [Symbol, String] Target name.

View File

@ -2,6 +2,19 @@ module Rscons
module Util module Util
class << self 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. # Make a relative path corresponding to a possibly absolute one.
# #
# @param path [String] # @param path [String]
@ -10,7 +23,7 @@ module Rscons
# @return [String] # @return [String]
# Relative path. # Relative path.
def make_relative_path(path) def make_relative_path(path)
if Rscons.absolute_path?(path) if absolute_path?(path)
if path =~ %r{^(\w):(.*)$} if path =~ %r{^(\w):(.*)$}
"_#{$1}#{$2}" "_#{$1}#{$2}"
else else

View File

@ -1,6 +1,34 @@
module Rscons module Rscons
describe Util do 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 describe ".make_relative_path" do
context "when passed a relative path" do context "when passed a relative path" do
it "returns the path itself" do it "returns the path itself" do
@ -10,7 +38,7 @@ module Rscons
context "when passed an absolute path" do context "when passed an absolute path" do
before(:each) do before(:each) do
expect(Rscons).to receive(:absolute_path?).and_return(true) expect(Util).to receive(:absolute_path?).and_return(true)
end end
context "on Windows" do context "on Windows" do
@ -19,7 +47,7 @@ module Rscons
end end
end end
context "on POSIX" do context "on non-Windows" do
it "returns a relative path corresponding to an absolute one" do it "returns a relative path corresponding to an absolute one" do
expect(Util.make_relative_path("/foo/bar")).to eq "_/foo/bar" expect(Util.make_relative_path("/foo/bar")).to eq "_/foo/bar"
end end

View File

@ -1,31 +1,4 @@
describe Rscons do 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 describe ".set_suffix" do
it "changes the suffix to the new one" do it "changes the suffix to the new one" do
@ -125,4 +98,5 @@ describe Rscons do
end end
end end
end end
end end