win32-xpath 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f386aff8075f1869d7b1d5931145726e2f741a192c96a8ecb214adb95e57775
4
- data.tar.gz: 5964f619e3c44bfcd183a9b9368dad576dc72ae5dba21b566fe372a9645d7a12
3
+ metadata.gz: 3a5dc7b8cc4b32f0a194fd62a9c727ab8bed0eebf29b8923ff233de95230830a
4
+ data.tar.gz: 6259f5d6e3c91df4397946658d38a82c7dd3d3d862680819a15e87b0d3caffca
5
5
  SHA512:
6
- metadata.gz: 62429d580dafba1c74a90ba822365738ecadc6f00d231cb31e2a1665a955eb249dbe355914cce275613f3724aeba45aa11371470fc59b03b358a17defb20b6b6
7
- data.tar.gz: f2d0ee1f647ea21b18854d19a1490dbf51e0335fd25925d4c08c4f6a21240c7a9ee400123a4cf554131cf154350d2f517440e9adc6801c72be886500e268bd41
6
+ metadata.gz: babfea80ca48c92b191ec5179ebe3088a8f3a0a05b25191f67d1ea1f8ec4a29573dd347236e858ce7cd6a2515cc31729f19e38f4b944a7dba9b5ad93cca5ed81
7
+ data.tar.gz: 50468708592d798e43fb430eba9061ee40b8a9f01090eaa06095d2bd821fc931f62d09eb95515276bca79116d1509d87fbd719c2fd2d2bfcfdae674727169a98
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGES.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 1.2.0 - 9-Jul-2022
2
+ * Convert tests from test-unit to rspec, and update gemspec and Rakefile accordingly.
3
+
1
4
  ## 1.1.2 - 8-Aug-2021
2
5
  * The relative path directory argument now accepts anything that responds
3
6
  to the to_path method.
data/Gemfile CHANGED
@@ -1,3 +1,2 @@
1
- source 'https://rubygems.org' do
2
- gemspec
3
- end
1
+ source 'https://rubygems.org'
2
+ gemspec
data/README.md CHANGED
@@ -4,6 +4,9 @@ A custom `File.expand_path` method for Ruby on Windows that's much faster and wo
4
4
  ## Installation
5
5
  `gem install win32-xpath`
6
6
 
7
+ ## Adding the Trusted Cert
8
+ `gem cert --add <(curl -Ls https://raw.githubusercontent.com/djberg96/win32-xpath/main/certs/djberg96_pub.pem)`
9
+
7
10
  ## Synopsis
8
11
  ```ruby
9
12
  require 'win32/xpath'
@@ -53,7 +56,7 @@ Park Heesob for encoding advice and help.
53
56
  Apache-2.0
54
57
 
55
58
  ## Copyright
56
- (C) 2003-2019 Daniel J. Berger, All Rights Reserved
59
+ (C) 2003-2022 Daniel J. Berger, All Rights Reserved
57
60
 
58
61
  ## Warranty
59
62
  This package is provided "as is" and without any express or
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rake'
2
2
  require 'rake/clean'
3
- require 'rake/testtask'
4
3
  require 'rbconfig'
4
+ require 'rspec/core/rake_task'
5
5
  include RbConfig
6
6
 
7
7
  CLEAN.include(
@@ -38,7 +38,7 @@ namespace :gem do
38
38
  require 'rubygems/package'
39
39
  spec = Gem::Specification.load('win32-xpath.gemspec')
40
40
  spec.signing_key = File.join(Dir.home, '.ssh', 'gem-private_key.pem')
41
- Gem::Package.build(spec, true)
41
+ Gem::Package.build(spec)
42
42
  end
43
43
 
44
44
  task "Install the win32-xpath gem"
@@ -48,15 +48,14 @@ namespace :gem do
48
48
  end
49
49
  end
50
50
 
51
- Rake::TestTask.new do |t|
52
- task :test => [:build]
53
- t.test_files = FileList['test/*']
54
- t.libs << 'ext'
55
- end
56
-
57
51
  desc "Run benchmarks"
58
52
  task :bench => [:build] do
59
53
  ruby "-Iext bench/bench_win32_xpath.rb"
60
54
  end
61
55
 
62
- task :default => :test
56
+ desc "Run the test suite"
57
+ RSpec::Core::RakeTask.new(:spec) do |t|
58
+ t.rspec_opts = '-Iext'
59
+ end
60
+
61
+ task :default => [:build, :spec]
@@ -1,6 +1,6 @@
1
1
  require 'benchmark'
2
2
 
3
- MAX = 200000
3
+ MAX = 400000
4
4
  puts "\nOld File.expand_path"
5
5
 
6
6
  Benchmark.bm(30) do |x|
@@ -0,0 +1,213 @@
1
+ require 'rspec'
2
+ require 'tmpdir'
3
+ require 'win32/xpath'
4
+ require 'etc'
5
+
6
+ RSpec.describe 'win32-xpath' do
7
+ let!(:login) { Etc.getlogin }
8
+ let!(:env){ ENV.to_h }
9
+
10
+ before do
11
+ @pwd = Dir.pwd
12
+ @tmp = 'C:/Temp'
13
+ @root = 'C:/'
14
+ @drive = env['HOMEDRIVE']
15
+ @home = env['HOME'].tr('\\', '/')
16
+ @unc = "//foo/bar"
17
+ ENV['HOME'] = env['USERPROFILE'] || Dir.home
18
+ end
19
+
20
+ after do
21
+ ENV.replace(env)
22
+ end
23
+
24
+ example "converts an empty pathname into absolute current pathname" do
25
+ expect(File.expand_path('')).to eq(@pwd)
26
+ end
27
+
28
+ example "converts '.' into absolute pathname using current directory" do
29
+ expect(File.expand_path('.')).to eq(@pwd)
30
+ end
31
+
32
+ example "converts relative path into absolute pathname using current directory" do
33
+ expect(File.expand_path('foo')).to eq(File.join(@pwd, 'foo'))
34
+ end
35
+
36
+ example "converts relative path into absolute pathname ignoring nil dir" do
37
+ expect(File.expand_path('foo', nil)).to eq(File.join(@pwd, 'foo'))
38
+ end
39
+
40
+ example "converts relative path and directory into the expected absolute pathname" do
41
+ expect(File.expand_path('foo', 'bar')).to eq(File.join(@pwd, 'bar', 'foo'))
42
+ end
43
+
44
+ example "converts relative edge case pathnames into absolute pathnames" do
45
+ expect(File.expand_path('a.')).to eq(File.join(@pwd, 'a'))
46
+ expect(File.expand_path('.a')).to eq(File.join(@pwd, '.a'))
47
+ expect(File.expand_path('..a')).to eq(File.join(@pwd, '..a'))
48
+ expect(File.expand_path('a../b')).to eq(File.join(@pwd, 'a../b'))
49
+ end
50
+
51
+ example "converts a double dot pathname into the expected absolute pathname" do
52
+ expect( File.expand_path('a..')).to eq(File.join(@pwd, 'a'))
53
+ end
54
+
55
+ example "converts a pathname to an absolute pathname using a complete path" do
56
+ expect(File.expand_path('', @tmp)).to eq(@tmp)
57
+ expect(File.expand_path('a', @tmp)).to eq(File.join(@tmp, 'a'))
58
+ expect(File.expand_path('../a', "#{@tmp}/xxx")).to eq(File.join(@tmp, 'a'))
59
+ expect(File.expand_path('.', @root)).to eq(@root)
60
+ end
61
+
62
+ example "ignores supplied dir if path contains a drive letter" do
63
+ expect(File.expand_path(@root, "D:/")).to eq(@root)
64
+ end
65
+
66
+ example "removes trailing slashes from absolute path" do
67
+ expect(File.expand_path("#{@root}foo/")).to eq(File.join(@root, 'foo'))
68
+ expect(File.expand_path("#{@root}foo.rb/")).to eq(File.join(@root, 'foo.rb'))
69
+ end
70
+
71
+ example "removes trailing slashes from relative path" do
72
+ expect(File.expand_path("foo/")).to eq(File.join(@pwd, 'foo'))
73
+ expect(File.expand_path("foo//")).to eq(File.join(@pwd, 'foo'))
74
+ expect(File.expand_path("foo\\\\\\")).to eq(File.join(@pwd, 'foo'))
75
+ end
76
+
77
+ example "removes trailing spaces from absolute path" do
78
+ expect(File.expand_path("#{@root}foo ")).to eq(File.join(@root, 'foo'))
79
+ end
80
+
81
+ example "removes trailing dots from absolute path" do
82
+ expect(File.expand_path("#{@root}a.")).to eq(File.join(@root, 'a'))
83
+ end
84
+
85
+ example "converts a pathname with a drive letter but no slash" do
86
+ expect(File.expand_path("c:")).to match(/\Ac:\//i)
87
+ end
88
+
89
+ example "converts a pathname with a drive letter ignoring different drive dir" do
90
+ expect(File.expand_path("c:foo", "d:/bar")).to match(/\Ac:\//i)
91
+ end
92
+
93
+ example "converts a pathname which starts with a slash using current drive" do
94
+ expect(File.expand_path('/foo')).to match(/\A#{@drive}\/foo\z/i)
95
+ end
96
+
97
+ example "returns tainted strings or not" do
98
+ skip "Skipping on Ruby 2.7+" if RUBY_VERSION.to_f >= 2.7
99
+ expect(File.expand_path('foo').tainted?).to be true
100
+ expect(File.expand_path('foo'.taint).tainted?).to be true
101
+ expect(File.expand_path('/foo').tainted?).to be true
102
+ expect(File.expand_path('/foo'.taint).tainted?).to be true
103
+ expect(File.expand_path('C:/foo'.taint).tainted?).to be true
104
+ expect(File.expand_path('C:/foo').tainted?).to be false
105
+ expect(File.expand_path('//foo').tainted?).to be false
106
+ end
107
+
108
+ example "converts a pathname to an absolute pathname using tilde as base" do
109
+ expect(File.expand_path('~')).to eq(@home)
110
+ expect(File.expand_path('~/foo')).to eq("#{@home}/foo")
111
+ expect(File.expand_path('~/.foo')).to eq("#{@home}/.foo")
112
+ end
113
+
114
+ example "converts a pathname to an absolute pathname using tilde for UNC path" do
115
+ ENV['HOME'] = @unc
116
+ expect(File.expand_path('~')).to eq(@unc)
117
+ end
118
+
119
+ example "converts a tilde to path if used for dir argument" do
120
+ expect(File.expand_path('', '~')).to eq(@home)
121
+ expect(File.expand_path('', '~/foo')).to eq("#{@home}/foo")
122
+ expect(File.expand_path('foo', '~')).to eq("#{@home}/foo")
123
+ end
124
+
125
+ example "doesn't attempt to expand a tilde unless it's the first character" do
126
+ expect(File.expand_path("C:/Progra~1")).to eq("C:/Progra~1")
127
+ expect(File.expand_path("C:/Progra~1", "C:/Progra~1")).to eq("C:/Progra~1")
128
+ end
129
+
130
+ example "does not modify a HOME string argument" do
131
+ str = "~/a"
132
+ expect(File.expand_path(str)).to eq("#{@home}/a")
133
+ expect(str).to eq("~/a")
134
+ end
135
+
136
+ example "defaults to HOMEDRIVE + HOMEPATH if HOME or USERPROFILE are nil" do
137
+ ENV['HOME'] = nil
138
+ ENV['USERPROFILE'] = nil
139
+ ENV['HOMEDRIVE'] = "C:"
140
+ ENV['HOMEPATH'] = "\\Users\\foo"
141
+ expect(File.expand_path("~/bar")).to eq("C:/Users/foo/bar")
142
+ end
143
+
144
+ example "raises ArgumentError when HOME is nil" do
145
+ ENV['HOME'] = nil
146
+ ENV['USERPROFILE'] = nil
147
+ ENV['HOMEDRIVE'] = nil
148
+ expect{ File.expand_path('~') }.to raise_error(ArgumentError)
149
+ end
150
+
151
+ example "raises ArgumentError if HOME is relative" do
152
+ ENV['HOME'] = '.'
153
+ expect{ File.expand_path('~') }.to raise_error(ArgumentError)
154
+ end
155
+
156
+ example "raises ArgumentError if relative HOME path with tilde + user is provided" do
157
+ ENV['HOME'] = 'whatever'
158
+ expect{ File.expand_path('~anything') }.to raise_error(ArgumentError)
159
+ end
160
+
161
+ example "raises an ArgumentError if a bogus username is supplied" do
162
+ expect{ File.expand_path('~anything') }.to raise_error(ArgumentError)
163
+ end
164
+
165
+ example "converts a tilde plus username as expected" do
166
+ expect(File.expand_path("~#{login}")).to eq("C:/Users/#{login}")
167
+ expect(File.expand_path("~#{login}/foo")).to eq("C:/Users/#{login}/foo")
168
+ end
169
+
170
+ example "raises a TypeError if not passed a string" do
171
+ expect{ File.expand_path(1) }.to raise_error(TypeError)
172
+ expect{ File.expand_path(nil) }.to raise_error(TypeError)
173
+ expect{ File.expand_path(true) }.to raise_error(TypeError)
174
+ end
175
+
176
+ example "canonicalizes absolute path" do
177
+ expect(File.expand_path("C:/./dir")).to eq("C:/dir")
178
+ end
179
+
180
+ example "does not modify its argument" do
181
+ str = "./a/b/../c"
182
+ expect(File.expand_path(str, @home)).to eq("#{@home}/a/c")
183
+ expect(str).to eq("./a/b/../c")
184
+ end
185
+
186
+ example "accepts objects that have a to_path method" do
187
+ klass = Class.new{ def to_path; "a/b/c"; end }
188
+ obj = klass.new
189
+ expect(File.expand_path(obj)).to eq("#{@pwd}/a/b/c")
190
+ end
191
+
192
+ example "accepts objects that have a to_path method for relative dir argument" do
193
+ klass = Class.new{ def to_path; "bar"; end }
194
+ obj = klass.new
195
+ expect(File.expand_path('foo', obj)).to eq("#{@pwd}/bar/foo")
196
+ end
197
+
198
+ example "works with unicode characters" do
199
+ expect( File.expand_path("Ελλάσ")).to eq("#{@pwd}/Ελλάσ")
200
+ end
201
+
202
+ # The +1 below is for the slash trailing @pwd that's appended.
203
+ example "handles paths longer than 260 (MAX_PATH) characters" do
204
+ expect{ File.expand_path("a" * 261) }.not_to raise_error
205
+ expect{ File.expand_path("a" * 1024) }.not_to raise_error
206
+ expect(File.expand_path("a" * 1024).size).to eq(@pwd.size + 1024 + 1)
207
+ end
208
+
209
+ example "handles very long paths with tilde" do
210
+ path = "a * 1024"
211
+ expect{ File.expand_path("~/#{path}") }.not_to raise_error
212
+ end
213
+ end
data/win32-xpath.gemspec CHANGED
@@ -2,27 +2,28 @@ require 'rubygems'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'win32-xpath'
5
- spec.version = '1.1.2'
5
+ spec.version = '1.2.0'
6
6
  spec.author = 'Daniel J. Berger'
7
7
  spec.license = 'Apache-2.0'
8
8
  spec.email = 'djberg96@gmail.com'
9
9
  spec.homepage = 'http://github.com/djberg96/win32-xpath'
10
10
  spec.summary = 'Revamped File.expand_path for Windows'
11
- spec.test_file = 'test/test_win32_xpath.rb'
11
+ spec.test_file = 'spec/win32_xpath_spec.rb'
12
12
  spec.files = Dir['**/*'].reject{ |f| f.include?('git') }
13
13
  spec.cert_chain = Dir['certs/*']
14
14
 
15
15
  spec.extensions = ['ext/extconf.rb']
16
16
  spec.add_development_dependency('rake')
17
- spec.add_development_dependency('test-unit', '~> 3.4')
17
+ spec.add_development_dependency('rspec', '~> 3.11')
18
18
 
19
19
  spec.metadata = {
20
- 'homepage_uri' => 'https://github.com/djberg96/win32-xpath',
21
- 'bug_tracker_uri' => 'https://github.com/djberg96/win32-xpath/issues',
22
- 'changelog_uri' => 'https://github.com/djberg96/win32-xpath/blob/main/CHANGES.md',
23
- 'documentation_uri' => 'https://github.com/djberg96/win32-xpath/wiki',
24
- 'source_code_uri' => 'https://github.com/djberg96/win32-xpath',
25
- 'wiki_uri' => 'https://github.com/djberg96/win32-xpath/wiki'
20
+ 'homepage_uri' => 'https://github.com/djberg96/win32-xpath',
21
+ 'bug_tracker_uri' => 'https://github.com/djberg96/win32-xpath/issues',
22
+ 'changelog_uri' => 'https://github.com/djberg96/win32-xpath/blob/main/CHANGES.md',
23
+ 'documentation_uri' => 'https://github.com/djberg96/win32-xpath/wiki',
24
+ 'source_code_uri' => 'https://github.com/djberg96/win32-xpath',
25
+ 'wiki_uri' => 'https://github.com/djberg96/win32-xpath/wiki',
26
+ 'rubygems_mfa_required' => 'true'
26
27
  }
27
28
 
28
29
  spec.description = <<-EOF
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: win32-xpath
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Berger
@@ -35,7 +35,7 @@ cert_chain:
35
35
  ORVCZpRuCPpmC8qmqxUnARDArzucjaclkxjLWvCVHeFa9UP7K3Nl9oTjJNv+7/jM
36
36
  WZs4eecIcUc4tKdHxcAJ0MO/Dkqq7hGaiHpwKY76wQ1+8xAh
37
37
  -----END CERTIFICATE-----
38
- date:
38
+ date: 2022-07-09 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: rake
@@ -52,19 +52,19 @@ dependencies:
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
54
  - !ruby/object:Gem::Dependency
55
- name: test-unit
55
+ name: rspec
56
56
  requirement: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '3.4'
60
+ version: '3.11'
61
61
  type: :development
62
62
  prerelease: false
63
63
  version_requirements: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: '3.4'
67
+ version: '3.11'
68
68
  description: |2
69
69
  The win32-xpath library provides a revamped File.expand_path method
70
70
  for MS Windows. It is significantly faster, and supports ~user
@@ -75,25 +75,19 @@ extensions:
75
75
  - ext/extconf.rb
76
76
  extra_rdoc_files: []
77
77
  files:
78
+ - CHANGES.md
79
+ - Gemfile
80
+ - LICENSE
81
+ - MANIFEST.md
82
+ - README.md
83
+ - Rakefile
78
84
  - appveyor.yml
79
- - bench
80
85
  - bench/bench_win32_xpath.rb
81
- - certs
82
86
  - certs/djberg96_pub.pem
83
- - CHANGES.md
84
- - ext
85
87
  - ext/extconf.rb
86
- - ext/win32
87
88
  - ext/win32/xpath.c
88
- - Gemfile
89
- - lib
90
89
  - lib/win32-xpath.rb
91
- - LICENSE
92
- - MANIFEST.md
93
- - Rakefile
94
- - README.md
95
- - test
96
- - test/test_win32_xpath.rb
90
+ - spec/win32_xpath_spec.rb
97
91
  - win32-xpath.gemspec
98
92
  homepage: http://github.com/djberg96/win32-xpath
99
93
  licenses:
@@ -105,6 +99,7 @@ metadata:
105
99
  documentation_uri: https://github.com/djberg96/win32-xpath/wiki
106
100
  source_code_uri: https://github.com/djberg96/win32-xpath
107
101
  wiki_uri: https://github.com/djberg96/win32-xpath/wiki
102
+ rubygems_mfa_required: 'true'
108
103
  post_install_message:
109
104
  rdoc_options: []
110
105
  require_paths:
@@ -120,9 +115,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
115
  - !ruby/object:Gem::Version
121
116
  version: '0'
122
117
  requirements: []
123
- rubygems_version: 3.2.19
118
+ rubygems_version: 3.3.17
124
119
  signing_key:
125
120
  specification_version: 4
126
121
  summary: Revamped File.expand_path for Windows
127
122
  test_files:
128
- - test/test_win32_xpath.rb
123
+ - spec/win32_xpath_spec.rb
metadata.gz.sig CHANGED
Binary file
@@ -1,224 +0,0 @@
1
- require 'test-unit'
2
- require 'tmpdir'
3
- require 'win32/xpath'
4
- require 'etc'
5
-
6
- class Test_XPath < Test::Unit::TestCase
7
- def self.startup
8
- ENV['HOME'] ||= ENV['USERPROFILE']
9
- @@login = Etc.getlogin
10
- end
11
-
12
- def setup
13
- @pwd = Dir.pwd
14
- @tmp = 'C:/Temp'
15
- @root = 'C:/'
16
- @drive = ENV['HOMEDRIVE']
17
- @home = ENV['HOME'].tr('\\', '/')
18
- @unc = "//foo/bar"
19
- end
20
-
21
- test "converts an empty pathname into absolute current pathname" do
22
- assert_equal(@pwd, File.expand_path(''))
23
- end
24
-
25
- test "converts '.' into absolute pathname using current directory" do
26
- assert_equal(@pwd, File.expand_path('.'))
27
- end
28
-
29
- test "converts 'foo' into absolute pathname using current directory" do
30
- assert_equal(File.join(@pwd, 'foo'), File.expand_path('foo'))
31
- end
32
-
33
- test "converts 'foo' into absolute pathname ignoring nil dir" do
34
- assert_equal(File.join(@pwd, 'foo'), File.expand_path('foo', nil))
35
- end
36
-
37
- test "converts 'foo' and 'bar' into absolute pathname" do
38
- assert_equal(File.join(@pwd, "bar", "foo"), File.expand_path('foo', 'bar'))
39
- end
40
-
41
- test "converts a pathname into absolute pathname" do
42
- assert_equal(File.join(@pwd, 'a'), File.expand_path('a.'))
43
- assert_equal(File.join(@pwd, '.a'), File.expand_path('.a'))
44
- assert_equal(File.join(@pwd, '..a'), File.expand_path('..a'))
45
- assert_equal(File.join(@pwd, 'a../b'), File.expand_path('a../b'))
46
- end
47
-
48
- test "converts a pathname and make it valid" do
49
- assert_equal(File.join(@pwd, 'a'), File.expand_path('a..'))
50
- end
51
-
52
- test "converts a pathname to an absolute pathname using a complete path" do
53
- assert_equal(@tmp, File.expand_path('', @tmp))
54
- assert_equal(File.join(@tmp, 'a'), File.expand_path('a', @tmp))
55
- assert_equal(File.join(@tmp, 'a'), File.expand_path('../a', "#{@tmp}/xxx"))
56
- assert_equal(@root, File.expand_path('.', @root))
57
- end
58
-
59
- test "ignores supplied dir if path contains a drive letter" do
60
- assert_equal(@root, File.expand_path(@root, "D:/"))
61
- end
62
-
63
- test "removes trailing slashes from absolute path" do
64
- assert_equal(File.join(@root, 'foo'), File.expand_path("#{@root}foo/"))
65
- assert_equal(File.join(@root, 'foo.rb'), File.expand_path("#{@root}foo.rb/"))
66
- end
67
-
68
- test "removes trailing slashes from relative path" do
69
- assert_equal(File.join(@pwd, 'foo'), File.expand_path("foo/"))
70
- assert_equal(File.join(@pwd, 'foo'), File.expand_path("foo//"))
71
- assert_equal(File.join(@pwd, 'foo'), File.expand_path("foo\\\\\\"))
72
- end
73
-
74
- test "removes trailing spaces from absolute path" do
75
- assert_equal(File.join(@root, 'foo'), File.expand_path("#{@root}foo "))
76
- end
77
-
78
- test "removes trailing dots from absolute path" do
79
- assert_equal(File.join(@root, 'a'), File.expand_path("#{@root}a."))
80
- end
81
-
82
- test "converts a pathname with a drive letter but no slash" do
83
- assert_match(/\Ac:\//i, File.expand_path("c:"))
84
- end
85
-
86
- test "converts a pathname with a drive letter ignoring different drive dir" do
87
- assert_match(/\Ac:\//i, File.expand_path("c:foo", "d:/bar"))
88
- end
89
-
90
- test "converts a pathname which starts with a slash using current drive" do
91
- assert_match(/\A#{@drive}\/foo\z/i, File.expand_path('/foo'))
92
- end
93
-
94
- test "returns tainted strings or not" do
95
- omit_if(RUBY_VERSION.to_f >= 2.7, "skipping taint checks on Ruby 2.7+")
96
- assert_true(File.expand_path('foo').tainted?)
97
- assert_true(File.expand_path('foo'.taint).tainted?)
98
- assert_true(File.expand_path('/foo').tainted?)
99
- assert_true(File.expand_path('/foo'.taint).tainted?)
100
- assert_true(File.expand_path('C:/foo'.taint).tainted?)
101
- assert_false(File.expand_path('C:/foo').tainted?)
102
- assert_false(File.expand_path('//foo').tainted?)
103
- end
104
-
105
- test "converts a pathname to an absolute pathname using tilde as base" do
106
- assert_equal(@home, File.expand_path('~'))
107
- assert_equal("#{@home}/foo", File.expand_path('~/foo'))
108
- assert_equal("#{@home}/.foo", File.expand_path('~/.foo'))
109
- end
110
-
111
- test "converts a pathname to an absolute pathname using tilde for UNC path" do
112
- ENV['HOME'] = @unc
113
- assert_equal(@unc, File.expand_path('~'))
114
- end
115
-
116
- test "converts a tilde to path if used for dir argument" do
117
- assert_equal(@home, File.expand_path('', '~'))
118
- assert_equal("#{@home}/foo", File.expand_path('', '~/foo'))
119
- assert_equal("#{@home}/foo", File.expand_path('foo', '~'))
120
- end
121
-
122
- test "doesn't attempt to expand a tilde unless it's the first character" do
123
- assert_equal("C:/Progra~1", File.expand_path("C:/Progra~1"))
124
- assert_equal("C:/Progra~1", File.expand_path("C:/Progra~1", "C:/Progra~1"))
125
- end
126
-
127
- test "does not modify a HOME string argument" do
128
- str = "~/a"
129
- assert_equal("#{@home}/a", File.expand_path(str))
130
- assert_equal("~/a", str)
131
- end
132
-
133
- test "defaults to HOMEDRIVE + HOMEPATH if HOME or USERPROFILE are nil" do
134
- ENV['HOME'] = nil
135
- ENV['USERPROFILE'] = nil
136
- ENV['HOMEDRIVE'] = "C:"
137
- ENV['HOMEPATH'] = "\\Users\\foo"
138
- assert_equal("C:/Users/foo/bar", File.expand_path("~/bar"))
139
- end
140
-
141
- test "raises ArgumentError when HOME is nil" do
142
- ENV['HOME'] = nil
143
- ENV['USERPROFILE'] = nil
144
- ENV['HOMEDRIVE'] = nil
145
- assert_raise(ArgumentError){ File.expand_path('~') }
146
- end
147
-
148
- test "raises ArgumentError if HOME is relative" do
149
- ENV['HOME'] = '.'
150
- assert_raise(ArgumentError){ File.expand_path('~') }
151
- end
152
-
153
- test "raises ArgumentError if relative user is provided" do
154
- ENV['HOME'] = '.'
155
- assert_raise(ArgumentError){ File.expand_path('~anything') }
156
- end
157
-
158
- test "raises an ArgumentError if a bogus username is supplied" do
159
- assert_raise(ArgumentError){ File.expand_path('~anything') }
160
- end
161
-
162
- test "converts a tilde plus username as expected" do
163
- assert_equal("C:/Users/#{@@login}", File.expand_path("~#{@@login}"))
164
- assert_equal("C:/Users/#{@@login}/foo", File.expand_path("~#{@@login}/foo"))
165
- end
166
-
167
- test "raises a TypeError if not passed a string" do
168
- assert_raise(TypeError){ File.expand_path(1) }
169
- assert_raise(TypeError){ File.expand_path(nil) }
170
- assert_raise(TypeError){ File.expand_path(true) }
171
- end
172
-
173
- test "canonicalizes absolute path" do
174
- assert_equal("C:/dir", File.expand_path("C:/./dir"))
175
- end
176
-
177
- test "does not modify its argument" do
178
- str = "./a/b/../c"
179
- assert_equal("#{@home}/a/c", File.expand_path(str, @home))
180
- assert_equal("./a/b/../c", str)
181
- end
182
-
183
- test "accepts objects that have a to_path method for main argument" do
184
- klass = Class.new{ def to_path; "a/b/c"; end }
185
- obj = klass.new
186
- assert_equal("#{@pwd}/a/b/c", File.expand_path(obj))
187
- end
188
-
189
- test "accepts objects that have a to_path method for relative dir argument" do
190
- klass = Class.new{ def to_path; "bar"; end }
191
- obj = klass.new
192
- assert_equal("#{@pwd}/bar/foo", File.expand_path('foo', obj))
193
- end
194
-
195
- test "works with unicode characters" do
196
- assert_equal("#{@pwd}/Ελλάσ", File.expand_path("Ελλάσ"))
197
- end
198
-
199
- # The +1 below is for the slash trailing @pwd that's appended.
200
- test "handles paths longer than 260 (MAX_PATH) characters" do
201
- assert_nothing_raised{ File.expand_path("a" * 261) }
202
- assert_nothing_raised{ File.expand_path("a" * 1024) }
203
- assert_equal(@pwd.size + 1024 + 1, File.expand_path("a" * 1024).size)
204
- end
205
-
206
- test "handles very long paths with tilde" do
207
- path = "a * 1024"
208
- assert_nothing_raised{ File.expand_path("~/#{path}") }
209
- end
210
-
211
- def teardown
212
- @pwd = nil
213
- @unc = nil
214
- @dir = nil
215
- @root = nil
216
- @drive = nil
217
-
218
- ENV['HOME'] = @home
219
- end
220
-
221
- def self.shutdown
222
- @@login = nil
223
- end
224
- end