pa 1.3.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.travis.yml +0 -5
- data/CHANGELOG +6 -0
- data/Gemfile.lock +11 -12
- data/README.md +4 -9
- data/lib/pa.rb +191 -193
- data/lib/pa/cmd.rb +18 -9
- data/lib/pa/directory.rb +15 -17
- data/lib/pa/path.rb +126 -76
- data/lib/pa/state.rb +22 -24
- data/lib/pa/util.rb +114 -17
- data/lib/pa/version.rb +1 -1
- data/pa.gemspec +1 -0
- data/pa.watchr +3 -3
- data/spec/data/tmp/.gitkeep +0 -0
- data/spec/pa/cmd_spec.rb +10 -12
- data/spec/pa/directory_spec.rb +6 -11
- data/spec/pa/path_spec.rb +48 -42
- data/spec/pa_spec.rb +168 -155
- data/spec/spec_helper.rb +22 -20
- metadata +5 -7
- data/spec/pa/util_spec.rb +0 -13
data/lib/pa/util.rb
CHANGED
@@ -1,41 +1,138 @@
|
|
1
|
+
require "rbconfig"
|
2
|
+
|
1
3
|
class Pa
|
4
|
+
# In order to reduce the dependencies, this Util class contains some util functions.
|
2
5
|
class Util
|
3
6
|
module Concern
|
4
7
|
def included(base)
|
5
8
|
base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods)
|
6
|
-
base.send :include, const_get(:InstanceMethods) if const_defined?(:InstanceMethods)
|
7
9
|
end
|
8
10
|
end
|
9
11
|
|
10
12
|
class << self
|
11
|
-
#
|
12
|
-
#
|
13
|
+
# Extracts options from a set of arguments.
|
14
|
+
#
|
13
15
|
# @example
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
16
|
+
#
|
17
|
+
# dirs, o = Util.extract_options(["foo", "bar", {a: 1}], b: 2)
|
18
|
+
# -> ["foo", "bar"], {a: 1, b: 2}
|
19
|
+
#
|
20
|
+
# (dir,) o = Util.extract_options(["foo", {a: 1}])
|
21
|
+
# -> "foo", {a: 1}
|
17
22
|
#
|
18
23
|
# @return [Array<Array,Hash>]
|
19
|
-
def extract_options(
|
20
|
-
if
|
21
|
-
[
|
24
|
+
def extract_options(args, default={})
|
25
|
+
if args.last.instance_of?(Hash)
|
26
|
+
[args[0...-1], default.merge(args[-1])]
|
22
27
|
else
|
23
|
-
[
|
28
|
+
[args, default]
|
24
29
|
end
|
25
30
|
end
|
26
31
|
|
27
|
-
|
28
|
-
|
32
|
+
# Wraps its argument in an array unless it is already an array (or array-like).
|
33
|
+
#
|
34
|
+
# Specifically:
|
35
|
+
#
|
36
|
+
# * If the argument is +nil+ an empty list is returned.
|
37
|
+
# * Otherwise, if the argument responds to +to_ary+ it is invoked, and its result returned.
|
38
|
+
# * Otherwise, returns an array with the argument as its single element.
|
39
|
+
#
|
40
|
+
# Array.wrap(nil) # => []
|
41
|
+
# Array.wrap([1, 2, 3]) # => [1, 2, 3]
|
42
|
+
# Array.wrap(0) # => [0]
|
43
|
+
#
|
44
|
+
# This method is similar in purpose to <tt>Kernel#Array</tt>, but there are some differences:
|
45
|
+
#
|
46
|
+
# * If the argument responds to +to_ary+ the method is invoked. <tt>Kernel#Array</tt>
|
47
|
+
# moves on to try +to_a+ if the returned value is +nil+, but <tt>Array.wrap</tt> returns
|
48
|
+
# such a +nil+ right away.
|
49
|
+
# * If the returned value from +to_ary+ is neither +nil+ nor an +Array+ object, <tt>Kernel#Array</tt>
|
50
|
+
# raises an exception, while <tt>Array.wrap</tt> does not, it just returns the value.
|
51
|
+
# * It does not call +to_a+ on the argument, though special-cases +nil+ to return an empty array.
|
52
|
+
#
|
53
|
+
# The last point is particularly worth comparing for some enumerables:
|
54
|
+
#
|
55
|
+
# Array(:foo => :bar) # => [[:foo, :bar]]
|
56
|
+
# Array.wrap(:foo => :bar) # => [{:foo => :bar}]
|
57
|
+
#
|
58
|
+
# Array("foo\nbar") # => ["foo\n", "bar"], in Ruby 1.8
|
59
|
+
# Array.wrap("foo\nbar") # => ["foo\nbar"]
|
60
|
+
#
|
61
|
+
# There's also a related idiom that uses the splat operator:
|
62
|
+
#
|
63
|
+
# [*object]
|
64
|
+
#
|
65
|
+
# which returns <tt>[nil]</tt> for +nil+, and calls to <tt>Array(object)</tt> otherwise.
|
66
|
+
#
|
67
|
+
# Thus, in this case the behavior is different for +nil+, and the differences with
|
68
|
+
# <tt>Kernel#Array</tt> explained above apply to the rest of +object+s.
|
69
|
+
def wrap_array(object)
|
70
|
+
if object.nil?
|
71
|
+
[]
|
72
|
+
elsif object.respond_to?(:to_ary)
|
73
|
+
object.to_ary || [object]
|
74
|
+
else
|
75
|
+
[object]
|
76
|
+
end
|
29
77
|
end
|
30
78
|
|
31
|
-
|
32
|
-
|
33
|
-
|
79
|
+
# Slice a hash to include only the given keys. This is useful for
|
80
|
+
# limiting an options hash to valid keys before passing to a method:
|
81
|
+
#
|
82
|
+
# @example
|
83
|
+
#
|
84
|
+
# a = {a: 1, b: 2, c: 3}
|
85
|
+
# a.slaice(:a, :b) -> {a: 1, b: 2}
|
86
|
+
def slice(hash, *keys)
|
87
|
+
keys = keys.map! { |key| hash.convert_key(key) } if hash.respond_to?(:convert_key)
|
88
|
+
h = hash.class.new
|
89
|
+
keys.each { |k| h[k] = hash[k] if hash.has_key?(k) }
|
90
|
+
h
|
91
|
+
end
|
34
92
|
|
35
|
-
# join
|
36
|
-
|
93
|
+
# different to File.join.
|
94
|
+
#
|
95
|
+
# @example
|
96
|
+
#
|
97
|
+
# join_path(".", "foo") -> "foo" not "./foo"
|
98
|
+
#
|
99
|
+
def join_path(dir, *names)
|
37
100
|
dir == "." ? File.join(*names) : File.join(dir, *names)
|
38
101
|
end
|
102
|
+
|
103
|
+
def linux?
|
104
|
+
RbConfig::CONFIG["host_os"] =~ /linux|cygwin/
|
105
|
+
end
|
106
|
+
|
107
|
+
def mac?
|
108
|
+
RbConfig::CONFIG["host_os"] =~ /mac|darwin/
|
109
|
+
end
|
110
|
+
|
111
|
+
def bsd?
|
112
|
+
RbConfig::CONFIG["host_os"] =~ /bsd/
|
113
|
+
end
|
114
|
+
|
115
|
+
def windows?
|
116
|
+
RbConfig::CONFIG["host_os"] =~ /mswin|mingw/
|
117
|
+
end
|
118
|
+
|
119
|
+
def solaris?
|
120
|
+
RbConfig::CONFIG["host_os"] =~ /solaris|sunos/
|
121
|
+
end
|
122
|
+
|
123
|
+
# TODO: who knows what symbian returns?
|
124
|
+
def symbian?
|
125
|
+
RbConfig::CONFIG["host_os"] =~ /symbian/
|
126
|
+
end
|
127
|
+
|
128
|
+
def posix?
|
129
|
+
linux? or mac? or bsd? or solaris? or begin
|
130
|
+
fork do end
|
131
|
+
true
|
132
|
+
rescue NotImplementedError, NoMethodError
|
133
|
+
false
|
134
|
+
end
|
135
|
+
end
|
39
136
|
end
|
40
137
|
end
|
41
138
|
end
|
data/lib/pa/version.rb
CHANGED
data/pa.gemspec
CHANGED
data/pa.watchr
CHANGED
File without changes
|
data/spec/pa/cmd_spec.rb
CHANGED
@@ -2,9 +2,7 @@ require "spec_helper"
|
|
2
2
|
require "fileutils"
|
3
3
|
require "tmpdir"
|
4
4
|
|
5
|
-
|
6
|
-
public :_copy, :_touch, :_mkdir, :_mktmpname, :_rmdir, :_copy, :_move, :_ln
|
7
|
-
end
|
5
|
+
public_all_methods Pa::Cmd::ClassMethods
|
8
6
|
|
9
7
|
describe Pa do
|
10
8
|
before :all do
|
@@ -13,16 +11,16 @@ describe Pa do
|
|
13
11
|
Dir.chdir @tmpdir
|
14
12
|
end
|
15
13
|
|
16
|
-
after :all do
|
17
|
-
Dir.chdir @curdir
|
18
|
-
FileUtils.rm_r @tmpdir
|
19
|
-
end
|
20
|
-
|
21
14
|
# clean up directory after each run
|
22
15
|
after :each do
|
23
16
|
FileUtils.rm_r Dir.glob("*", File::FNM_DOTMATCH)-%w[. ..]
|
24
17
|
end
|
25
18
|
|
19
|
+
after :all do
|
20
|
+
Dir.chdir @curdir
|
21
|
+
FileUtils.rm_r @tmpdir
|
22
|
+
end
|
23
|
+
|
26
24
|
describe "#home2" do
|
27
25
|
it "works" do
|
28
26
|
Pa.home2.should == Dir.home
|
@@ -36,7 +34,7 @@ describe Pa do
|
|
36
34
|
end
|
37
35
|
|
38
36
|
it "works" do
|
39
|
-
output = capture
|
37
|
+
output = capture do
|
40
38
|
Pa._ln(:link, "_lna", "_lnb", :verbose => true)
|
41
39
|
end
|
42
40
|
|
@@ -86,7 +84,7 @@ describe Pa do
|
|
86
84
|
it "doesn't raise Errno::EEXIST with :force" do
|
87
85
|
lambda{ Pa.symln("file1", "file2", :force => true) }.should_not raise_error(Errno::EEXIST)
|
88
86
|
File.symlink?("file2").should be_true
|
89
|
-
File.readlink("file2").should == "file1"
|
87
|
+
File.readlink(File.absolute_path("file2")).should == "file1"
|
90
88
|
end
|
91
89
|
end
|
92
90
|
|
@@ -196,7 +194,7 @@ describe Pa do
|
|
196
194
|
|
197
195
|
path = Pa.mktmpdir("foo")
|
198
196
|
|
199
|
-
path.should =~ %r~#{Regexp.escape(
|
197
|
+
path.should =~ %r~#{Regexp.escape(Dir.tmpdir)}/foo~
|
200
198
|
end
|
201
199
|
end
|
202
200
|
|
@@ -547,7 +545,7 @@ describe Pa do
|
|
547
545
|
end
|
548
546
|
end
|
549
547
|
|
550
|
-
describe "
|
548
|
+
describe "DELEGATE_CLASS_METHODS" do
|
551
549
|
it "works" do
|
552
550
|
Pa.stub(:home2) { "/home/foo" }
|
553
551
|
Pa.home.should == Pa("/home/foo")
|
data/spec/pa/directory_spec.rb
CHANGED
@@ -2,11 +2,7 @@ require "spec_helper"
|
|
2
2
|
require "fileutils"
|
3
3
|
require "tmpdir"
|
4
4
|
|
5
|
-
|
6
|
-
class << self
|
7
|
-
public :_copy, :_move, :_rmdir, :_mktmpname, :_mkdir, :_touch
|
8
|
-
end
|
9
|
-
end
|
5
|
+
public_all_methods Pa
|
10
6
|
|
11
7
|
describe Pa do
|
12
8
|
before :all do
|
@@ -121,7 +117,6 @@ describe Pa do
|
|
121
117
|
break
|
122
118
|
}
|
123
119
|
end
|
124
|
-
|
125
120
|
end
|
126
121
|
|
127
122
|
describe ".each" do
|
@@ -276,20 +271,20 @@ describe Pa do
|
|
276
271
|
end
|
277
272
|
|
278
273
|
it "works" do
|
279
|
-
Pa.ls.should == %w[filea dira].map{|v|Pa(v)}
|
280
|
-
Pa.ls(Dir.pwd).should == %w[filea dira].map{|v|Pa(v)}
|
274
|
+
Pa.ls.sort.should == %w[filea dira].map{|v|Pa(v)}.sort
|
275
|
+
Pa.ls(Dir.pwd).sort.should == %w[filea dira].map{|v|Pa(v)}.sort
|
281
276
|
end
|
282
277
|
|
283
278
|
it "list multi paths" do
|
284
|
-
Pa.ls(".", "dira").should == %w[filea dira fileb].map{|v|Pa(v)}
|
279
|
+
Pa.ls(".", "dira").sort.should == %w[filea dira fileb].map{|v|Pa(v)}.sort
|
285
280
|
end
|
286
281
|
|
287
282
|
it "with :absolute => true" do
|
288
|
-
Pa.ls(:absolute => true).should == %w[filea dira].map{|v|Pa(File.join(Dir.pwd, v))}
|
283
|
+
Pa.ls(:absolute => true).sort.should == %w[filea dira].map{|v|Pa(File.join(Dir.pwd, v))}.sort
|
289
284
|
end
|
290
285
|
|
291
286
|
it "call a block" do
|
292
|
-
Pa.ls{|p, fn| p.directory? }.should == %w[dira].map{|v|Pa(v)}
|
287
|
+
Pa.ls{|p, fn| p.directory? }.sort.should == %w[dira].map{|v|Pa(v)}.sort
|
293
288
|
end
|
294
289
|
end
|
295
290
|
|
data/spec/pa/path_spec.rb
CHANGED
@@ -18,7 +18,7 @@ describe Pa do
|
|
18
18
|
describe ".dangling?" do
|
19
19
|
it "works" do
|
20
20
|
olddir=Dir.pwd
|
21
|
-
Dir.chdir("#{$
|
21
|
+
Dir.chdir("#{$spec_dir}/data/tmp")
|
22
22
|
|
23
23
|
begin
|
24
24
|
File.open("fa", "w"){|f| f.puts "guten" }
|
@@ -44,60 +44,66 @@ describe Pa do
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
describe ".
|
48
|
-
it "
|
49
|
-
Pa.
|
47
|
+
describe ".expand2" do
|
48
|
+
it "expand_path" do
|
49
|
+
Pa.expand2("~").should == File.expand_path("~")
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
describe ".base" do
|
60
|
-
it "works" do
|
61
|
-
Pa.base("/home/foo.bar", ext: true).should == [Pa("foo"), "bar"]
|
53
|
+
describe ".relative_to?" do
|
54
|
+
it do
|
55
|
+
expect(Pa.relative_to?("/home/foo", "/home")).to be_true
|
56
|
+
expect(Pa.relative_to?("/home1/foo", "/home")).to be_false
|
62
57
|
end
|
63
58
|
end
|
64
59
|
|
65
|
-
|
66
|
-
|
67
|
-
Pa.
|
68
|
-
|
60
|
+
describe ".relative_to2" do
|
61
|
+
it do
|
62
|
+
expect(Pa.relative_to2("/home/foo", "/home")).to eq("foo")
|
63
|
+
expect(Pa.relative_to2("/home/foo", "/home/foo")).to eq(".")
|
64
|
+
expect(Pa.relative_to2("/home/foo", "/bin")).to eq("/home/foo")
|
69
65
|
|
70
|
-
|
71
|
-
Pa.
|
72
|
-
|
66
|
+
expect(Pa.relative_to2("/home/foo", "/home/foo/")).to eq(".")
|
67
|
+
expect(Pa.relative_to2("/home/foo/", "/home/foo")).to eq(".")
|
68
|
+
|
69
|
+
expect(Pa.relative_to2("/home1/foo", "/home")).to eq("/home1/foo")
|
70
|
+
end
|
71
|
+
end
|
73
72
|
|
74
|
-
|
75
|
-
|
73
|
+
describe ".has_ext?" do
|
74
|
+
it do
|
75
|
+
expect(Pa.has_ext?("foo.txt", ".txt")).to be_true
|
76
|
+
expect(Pa.has_ext?("foo", ".txt")).to be_false
|
77
|
+
expect(Pa.has_ext?("foo.1txt", ".txt")).to be_false
|
76
78
|
end
|
77
79
|
end
|
78
80
|
|
79
|
-
describe ".
|
80
|
-
it
|
81
|
-
Pa.
|
81
|
+
describe ".delete_ext2" do
|
82
|
+
it do
|
83
|
+
expect(Pa.delete_ext2("foo.txt", ".txt")).to eq("foo")
|
84
|
+
expect(Pa.delete_ext2("foo", ".txt")).to eq("foo")
|
85
|
+
expect(Pa.delete_ext2("foo.epub", ".txt")).to eq("foo.epub")
|
82
86
|
end
|
83
87
|
end
|
84
88
|
|
85
|
-
describe ".
|
86
|
-
it
|
87
|
-
Pa.
|
89
|
+
describe ".add_ext2" do
|
90
|
+
it do
|
91
|
+
expect(Pa.add_ext2("foo", ".txt")).to eq("foo.txt")
|
92
|
+
expect(Pa.add_ext2("foo.txt", ".txt")).to eq("foo.txt")
|
93
|
+
expect(Pa.add_ext2("foo.epub", ".txt")).to eq("foo.epub.txt")
|
88
94
|
end
|
89
95
|
end
|
90
96
|
|
97
|
+
|
91
98
|
describe ".shorten2" do
|
92
|
-
|
93
|
-
|
94
|
-
Pa.shorten2("/home/
|
95
|
-
|
99
|
+
it do
|
100
|
+
Pa.stub(:home2) { "/home"}
|
101
|
+
expect(Pa.shorten2("/home/file")).to eq("~/file")
|
102
|
+
expect(Pa.shorten2("/home1/file")).to eq("/home1/file")
|
96
103
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
end
|
104
|
+
Pa.stub(:home2) { "" }
|
105
|
+
expect(Pa.shorten2("/home/file")).to eq("/home/file")
|
106
|
+
end
|
101
107
|
end
|
102
108
|
|
103
109
|
describe ".real2" do
|
@@ -118,24 +124,24 @@ describe Pa do
|
|
118
124
|
end
|
119
125
|
end
|
120
126
|
|
121
|
-
describe "
|
122
|
-
it
|
127
|
+
describe "DELEGATE_CLASS_METHODS" do
|
128
|
+
it do
|
123
129
|
Pa.should_receive(:pwd2).with(1,2)
|
124
130
|
|
125
131
|
Pa.pwd(1,2)
|
126
132
|
end
|
127
133
|
end
|
128
134
|
|
129
|
-
describe "
|
130
|
-
it
|
135
|
+
describe "DELEGATE_METHODS2" do
|
136
|
+
it do
|
131
137
|
Pa.should_receive(:parent2).with("foo", 1, 2)
|
132
138
|
|
133
139
|
Pa.new("foo").parent2(1, 2)
|
134
140
|
end
|
135
141
|
end
|
136
142
|
|
137
|
-
describe "
|
138
|
-
it
|
143
|
+
describe "DELEGATE_METHODS" do
|
144
|
+
it do
|
139
145
|
p = Pa.new("foo")
|
140
146
|
|
141
147
|
p.should_receive(:parent2).with(1,2)
|
data/spec/pa_spec.rb
CHANGED
@@ -1,40 +1,11 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
|
4
|
-
class <<self
|
5
|
-
public :_wrap, :build_path2
|
6
|
-
end
|
7
|
-
end
|
3
|
+
public_all_methods Pa
|
8
4
|
|
9
5
|
describe Pa do
|
10
6
|
it "._wrap" do
|
11
|
-
Pa._wrap("foo").
|
12
|
-
Pa._wrap(["guten", "tag"]).
|
13
|
-
end
|
14
|
-
|
15
|
-
describe ".build_path2" do
|
16
|
-
it "works" do
|
17
|
-
Pa.build_path2(path: "foo/bar.avi").should == "foo/bar.avi"
|
18
|
-
Pa.build_path2(dir: "foo", name: "bar", ext: "avi").should == "foo/bar.avi"
|
19
|
-
end
|
20
|
-
|
21
|
-
it "complex examples" do
|
22
|
-
Pa.build_path2(dir: "foo").should == "foo"
|
23
|
-
Pa.build_path2(fname: "bar.avi").should == "bar.avi"
|
24
|
-
Pa.build_path2(base: "bar.avi").should == "bar.avi"
|
25
|
-
Pa.build_path2(name: "bar").should == "bar"
|
26
|
-
Pa.build_path2(fext: ".avi").should == ".avi"
|
27
|
-
Pa.build_path2(ext: "avi").should == ".avi"
|
28
|
-
Pa.build_path2(dir: "", fname: "bar.avi").should == "bar.avi"
|
29
|
-
end
|
30
|
-
|
31
|
-
it "percedure" do
|
32
|
-
Pa.build_path2(path: "foo", fname: "bar").should == "foo"
|
33
|
-
Pa.build_path2(fname: "foo", name: "bar").should == "foo"
|
34
|
-
Pa.build_path2(fname: "foo", ext: "bar").should == "foo"
|
35
|
-
Pa.build_path2(fname: "foo", fext: ".bar").should == "foo"
|
36
|
-
Pa.build_path2(fext: "foo", ext: "bar").should == "foo"
|
37
|
-
end
|
7
|
+
expect(Pa._wrap("foo")).to eq(Pa("foo"))
|
8
|
+
expect(Pa._wrap(["guten", "tag"])).to eq([Pa("guten"), Pa("tag")])
|
38
9
|
end
|
39
10
|
|
40
11
|
describe ".get" do
|
@@ -43,144 +14,219 @@ describe Pa do
|
|
43
14
|
def path.path
|
44
15
|
"hello"
|
45
16
|
end
|
46
|
-
Pa.get(path).
|
17
|
+
expect(Pa.get(path)).to eq("hello")
|
47
18
|
end
|
48
19
|
|
49
20
|
it "get path from a string" do
|
50
|
-
Pa.get("foo").
|
21
|
+
expect(Pa.get("foo")).to eq("foo")
|
51
22
|
end
|
52
23
|
|
53
24
|
it "get nil from nil" do
|
54
|
-
Pa.get(nil).
|
25
|
+
expect(Pa.get(nil)).to eq(nil)
|
55
26
|
end
|
56
27
|
|
57
28
|
it "otherwise raise ArgumentError" do
|
58
|
-
|
29
|
+
expect{ Pa.get([]) }.to raise_error(ArgumentError)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe ".absolute2" do
|
34
|
+
it do
|
35
|
+
expect(Pa.absolute2("a.txt")).to eq(File.join(File.absolute_path(".", "."), "a.txt")) # rbx
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe ".dir2" do
|
40
|
+
it do
|
41
|
+
expect(Pa.dir2("/home/foo.txt")).to eq("/home")
|
42
|
+
expect(Pa.dir2("foo.txt")).to eq(".")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe ".dir_strict2" do
|
47
|
+
it do
|
48
|
+
expect(Pa.dir_strict2("foo.txt")).to eq("")
|
49
|
+
expect(Pa.dir_strict2("./foo.txt")).to eq(".")
|
50
|
+
expect(Pa.dir_strict2("../foo.txt")).to eq("..")
|
51
|
+
expect(Pa.dir_strict2("/foo.txt")).to eq("/")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe ".base2" do
|
56
|
+
it do
|
57
|
+
expect(Pa.base2("foo.txt")).to eq("foo.txt")
|
58
|
+
expect(Pa.base2("/home/foo.txt")).to eq("foo.txt")
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
it "(ext: true)" do
|
63
|
+
expect(Pa.base2("/home/foo.bar", ext: true)).to eq(["foo", "bar"])
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe ".base" do
|
68
|
+
it do
|
69
|
+
expect(Pa.base("/home/foo.txt")).to eq(Pa("foo.txt"))
|
70
|
+
expect(Pa.base("/home/foo.bar", ext: true)).to eq([Pa("foo"), "bar"])
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe ".name2" do
|
75
|
+
it do
|
76
|
+
expect(Pa.name2("foo.txt")).to eq("foo")
|
77
|
+
expect(Pa.name2("/home/foo.txt")).to eq("foo")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe ".ext2" do
|
82
|
+
it do
|
83
|
+
expect(Pa.ext2("foo.txt")).to eq(".txt")
|
84
|
+
expect(Pa.ext2("foo")).to eq("")
|
85
|
+
expect(Pa.ext2("/home/foo.txt")).to eq(".txt")
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe ".fext2" do
|
90
|
+
it do
|
91
|
+
expect(Pa.fext2("foo.txt")).to eq("txt")
|
92
|
+
expect(Pa.fext2("foo")).to eq("")
|
93
|
+
expect(Pa.fext2("/home/foo.txt")).to eq("txt")
|
59
94
|
end
|
60
95
|
end
|
61
96
|
|
62
97
|
describe "split2" do
|
63
98
|
it "split a path into two part: dirname and basename" do
|
64
|
-
Pa.split2("/home/b/a.txt").
|
99
|
+
expect(Pa.split2("/home/b/a.txt")).to eq(["/home/b", "a.txt"])
|
65
100
|
end
|
66
101
|
|
67
102
|
it "with :all options: split all parts" do
|
68
|
-
Pa.split2("/home/b/a.txt", :all => true).
|
103
|
+
expect(Pa.split2("/home/b/a.txt", :all => true)).to eq(["/", "home", "b", "a.txt"])
|
69
104
|
end
|
70
105
|
end
|
71
106
|
|
72
107
|
describe "split" do
|
73
108
|
it "is a special case" do
|
74
|
-
Pa.split("/home/b/a.txt").
|
109
|
+
expect(Pa.split("/home/b/a.txt")).to eq([Pa("/home/b"), "a.txt"])
|
75
110
|
end
|
76
111
|
end
|
77
112
|
|
78
113
|
describe ".join2" do
|
79
114
|
it "join a path" do
|
80
|
-
Pa.join2("/a", "b").
|
115
|
+
expect(Pa.join2("/a", "b")).to eq("/a/b")
|
81
116
|
end
|
82
117
|
|
83
118
|
it "skip nil values" do
|
84
|
-
Pa.join2("/a", "b", nil).
|
119
|
+
expect(Pa.join2("/a", "b", nil)).to eq("/a/b")
|
85
120
|
end
|
86
121
|
|
87
122
|
it "skip empty values" do
|
88
|
-
Pa.join2("/a", "b", "").
|
123
|
+
expect(Pa.join2("/a", "b", "")).to eq("/a/b")
|
89
124
|
end
|
90
125
|
end
|
91
126
|
|
92
|
-
describe "
|
93
|
-
it
|
94
|
-
Pa.
|
95
|
-
Pa.
|
96
|
-
Pa.build2(path: "/home/guten.avi"){ |p| "#{p.dir}/foo.#{p.ext}" }.should == "/home/foo.avi"
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
describe "class DELEGATE_METHODS" do
|
101
|
-
it "works" do
|
102
|
-
Pa.stub(:build2){|arg| arg }
|
103
|
-
|
104
|
-
Pa.build("foo").should == Pa("foo")
|
127
|
+
describe "DELEGATE_CLASS_METHODS" do
|
128
|
+
it do
|
129
|
+
Pa.stub(:dir2) { "foo" }
|
130
|
+
expect(Pa.dir).to eq(Pa("foo"))
|
105
131
|
end
|
106
132
|
end
|
107
133
|
|
108
134
|
describe "#initilaize" do
|
109
135
|
it "support ~/foo path" do
|
110
|
-
Pa.new("~/foo").
|
136
|
+
expect(Pa.new("~/foo")).to eq(Pa("#{ENV['HOME']}/foo"))
|
111
137
|
end
|
112
138
|
end
|
113
139
|
|
114
|
-
|
115
|
-
|
116
|
-
|
140
|
+
describe "DELEGATE_ATTR_METHODS2" do
|
141
|
+
it do
|
142
|
+
Pa.stub(:dir2) { "foo" }
|
117
143
|
|
118
|
-
|
119
|
-
|
144
|
+
a = Pa("foo")
|
145
|
+
expect(a.dir2).to eq("foo")
|
146
|
+
expect(a.instance_variable_get(:@dir2)).to eq("foo")
|
147
|
+
end
|
120
148
|
end
|
121
149
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
150
|
+
describe "DELEGATE_ATTR_METHODS" do
|
151
|
+
it do
|
152
|
+
a = Pa("foo")
|
153
|
+
a.stub(:dir2) { "foo" }
|
154
|
+
|
155
|
+
expect(a.dir).to eq(Pa("foo"))
|
156
|
+
expect(a.instance_variable_get(:@dir)).to eq(Pa("foo"))
|
157
|
+
end
|
127
158
|
end
|
128
159
|
|
129
|
-
|
130
|
-
|
160
|
+
describe "DELEGATE_METHODS2" do
|
161
|
+
it do
|
162
|
+
Pa.stub(:join2) { "foo" }
|
163
|
+
|
164
|
+
expect(Pa("foo").join2).to eq("foo")
|
165
|
+
end
|
131
166
|
end
|
132
167
|
|
133
|
-
|
134
|
-
|
168
|
+
describe "DELEGATE_METHODS" do
|
169
|
+
it do
|
170
|
+
Pa.stub(:join2) { "foo" }
|
171
|
+
|
172
|
+
expect(Pa("foo").join).to eq(Pa("foo"))
|
173
|
+
end
|
135
174
|
end
|
136
175
|
|
137
|
-
|
138
|
-
|
139
|
-
|
176
|
+
describe "DELEGATE_TO_PATH2" do
|
177
|
+
it do
|
178
|
+
a = Pa("foo")
|
179
|
+
a.path.stub("sub"){ "bar" }
|
180
|
+
|
181
|
+
expect(a.sub2).to eq("bar")
|
182
|
+
end
|
140
183
|
end
|
141
184
|
|
142
|
-
|
143
|
-
|
144
|
-
|
185
|
+
describe "DELEGATE_TO_PATH" do
|
186
|
+
it do
|
187
|
+
a = Pa("foo")
|
188
|
+
a.path.stub("start_with?"){ "bar" }
|
189
|
+
|
190
|
+
expect(a.start_with?).to eq("bar")
|
191
|
+
end
|
145
192
|
end
|
146
193
|
|
147
|
-
|
148
|
-
|
194
|
+
describe "#inspect" do
|
195
|
+
it do
|
196
|
+
expect(Pa("/foo/bar.txt").inspect).to match(/path|absolute/)
|
197
|
+
end
|
149
198
|
end
|
150
199
|
|
151
|
-
|
152
|
-
|
200
|
+
describe "#to_s" do
|
201
|
+
it do
|
202
|
+
expect(Pa("bar.txt").to_s).to eq("bar.txt")
|
203
|
+
end
|
153
204
|
end
|
154
205
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
206
|
+
describe "#replace" do
|
207
|
+
it do
|
208
|
+
a = Pa("/home/guten")
|
209
|
+
a.replace "/bar/foo.txt"
|
210
|
+
|
211
|
+
expect(a.path ).to eq("/bar/foo.txt")
|
212
|
+
expect(a.absolute2).to eq("/bar/foo.txt")
|
213
|
+
expect(a.dir2 ).to eq("/bar")
|
214
|
+
expect(a.base2 ).to eq("foo.txt")
|
215
|
+
expect(a.name2 ).to eq("foo")
|
216
|
+
expect(a.ext2 ).to eq(".txt")
|
217
|
+
expect(a.fext2 ).to eq("txt")
|
218
|
+
end
|
167
219
|
end
|
168
220
|
|
169
221
|
describe "#<=>" do
|
170
222
|
it "runs ok" do
|
171
|
-
(Pa("/home/b") <=> Pa("/home/a")).
|
223
|
+
expect(Pa("/home/b") <=> Pa("/home/a")).to eq(1)
|
172
224
|
end
|
173
225
|
end
|
174
226
|
|
175
227
|
describe "#+" do
|
176
228
|
it "runs ok" do
|
177
|
-
(Pa("/home")+"~").
|
178
|
-
end
|
179
|
-
end
|
180
|
-
|
181
|
-
describe "#sub2" do
|
182
|
-
it "runs ok" do
|
183
|
-
Pa("/home/foo").sub2(/o/,"").should == "/hme/foo"
|
229
|
+
expect(Pa("/home")+"~").to eq(Pa("/home~"))
|
184
230
|
end
|
185
231
|
end
|
186
232
|
|
@@ -188,13 +234,7 @@ describe Pa do
|
|
188
234
|
it "runs ok" do
|
189
235
|
pa = Pa("/home/foo")
|
190
236
|
pa.sub!(/o/,"")
|
191
|
-
pa.
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
describe "#gsub2" do
|
196
|
-
it "runs ok" do
|
197
|
-
Pa("/home/foo").gsub2(/o/,"").should == "/hme/f"
|
237
|
+
expect(pa).to eq(Pa("/hme/foo"))
|
198
238
|
end
|
199
239
|
end
|
200
240
|
|
@@ -202,67 +242,40 @@ describe Pa do
|
|
202
242
|
it "runs ok" do
|
203
243
|
pa = Pa("/home/foo")
|
204
244
|
pa.gsub!(/o/,"")
|
205
|
-
pa.
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
describe "#match" do
|
210
|
-
it "runs ok" do
|
211
|
-
Pa("/home/foo").match(/foo/)[0].should == "foo"
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
describe "#start_with?" do
|
216
|
-
it "runs ok" do
|
217
|
-
Pa("/home/foo").start_with?("/home").should be_true
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
describe "#end_with?" do
|
222
|
-
it "runs ok" do
|
223
|
-
Pa("/home/foo").end_with?("foo").should be_true
|
245
|
+
expect(pa).to eq(Pa("/hme/f"))
|
224
246
|
end
|
225
247
|
end
|
226
248
|
|
227
249
|
describe "#=~" do
|
228
250
|
it "runs ok" do
|
229
|
-
(Pa("/home/foo") =~ /foo/).
|
251
|
+
expect((Pa("/home/foo") =~ /foo/)).to be_true
|
230
252
|
end
|
231
253
|
end
|
232
254
|
|
233
|
-
describe "#
|
234
|
-
|
235
|
-
Pa
|
236
|
-
Pa.new("/home/guten.avi").build2(dir: "foo").should == "foo/guten.avi"
|
237
|
-
Pa.new("/home/guten.avi").build2(fname: "bar").should == "/home/bar"
|
238
|
-
Pa.new("/home/guten.avi").build2(base: "bar").should == "/home/bar"
|
239
|
-
Pa.new("/home/guten.avi").build2(name: "bar").should == "/home/bar.avi"
|
240
|
-
Pa.new("/home/guten.avi").build2(ext: "ogg").should == "/home/guten.ogg"
|
241
|
-
Pa.new("/home/guten.avi").build2(fext: ".ogg").should == "/home/guten.ogg"
|
242
|
-
Pa.new("/home/guten.avi").build2(dir: "foo", name: "bar", ext: "ogg").should == "foo/bar.ogg"
|
243
|
-
end
|
244
|
-
|
245
|
-
it "percedure" do
|
246
|
-
Pa.new("/home/guten.avi").build2(path: "foo", fname: "bar").should == "foo"
|
247
|
-
Pa.new("/home/guten.avi").build2(fname: "foo", name: "bar").should == "/home/foo"
|
248
|
-
Pa.new("/home/guten.avi").build2(fname: "foo", ext: "ogg").should == "/home/foo"
|
249
|
-
Pa.new("/home/guten.avi").build2(fname: "foo", fext: ".ogg").should == "/home/foo"
|
250
|
-
Pa.new("/home/guten.avi").build2(fext: ".ogg", ext: "mp3").should == "/home/guten.ogg"
|
255
|
+
describe "#change2" do
|
256
|
+
before :all do
|
257
|
+
@a = Pa("/home/a.txt")
|
251
258
|
end
|
252
|
-
end
|
253
259
|
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
260
|
+
it do
|
261
|
+
expect(@a.change2(path: "foo")).to eq("foo")
|
262
|
+
expect(@a.change2(dir: "foo")).to eq("foo/a.txt")
|
263
|
+
expect(@a.change2(base: "bar")).to eq("/home/bar")
|
264
|
+
expect(@a.change2(name: "bar")).to eq("/home/bar.txt")
|
265
|
+
expect(@a.change2(ext: ".epub")).to eq("/home/a.epub")
|
258
266
|
end
|
259
|
-
end
|
260
267
|
|
261
|
-
|
262
|
-
|
263
|
-
|
268
|
+
it "(complex)" do
|
269
|
+
expect(@a.change2(dir: "foo", base: "bar")).to eq("foo/bar")
|
270
|
+
expect(@a.change2(dir: "foo", name: "bar", ext: ".epub")).to eq("foo/bar.epub")
|
271
|
+
expect(@a.change2(dir: "foo", name: "bar")).to eq("foo/bar.txt")
|
272
|
+
expect(@a.change2(name: "bar", ext: ".epub")).to eq("/home/bar.epub")
|
273
|
+
end
|
264
274
|
|
265
|
-
|
275
|
+
it "has a percudure" do
|
276
|
+
expect(@a.change2(path: "foo", dir: "bar")).to eq("foo")
|
277
|
+
expect(@a.change2(base: "foo", name: "bar")).to eq("/home/foo")
|
278
|
+
expect(@a.change2(base: "foo", ext: ".ogg")).to eq("/home/foo")
|
266
279
|
end
|
267
280
|
end
|
268
281
|
end
|