rubypath 0.1.0 → 0.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
  SHA1:
3
- metadata.gz: b9897ff8546e2a25f86e5a9feb820d2462755153
4
- data.tar.gz: 0d8c6cc18c678673c599aa6b45e316039d1c16b2
3
+ metadata.gz: 51f0799c1640f582b159084204c9e6e5168035fe
4
+ data.tar.gz: fa1da61036178cc9ad188abc93fd9016b468f0a2
5
5
  SHA512:
6
- metadata.gz: f95207874afd05e04c4878c4fb7df9e55f0e4548336551ebfdcbe31b103d830e6340861ab4f1fe3a2d5e06e8d5b776c4b7404cf771e269ac54549227fb9c50a5
7
- data.tar.gz: 9ea4c73e67e6361c4befa3ec2e37227241f80373631e5e380133a26fbc0f12ff32b78265e21bdbf950e335ecbd08dbe8bb991f49ad57d9d46397012edee2b55f
6
+ metadata.gz: 6092c75e0ea5910bd2b6cea271064e1354c74c06828612c90d6a10c56e53bdd7cf31d146f433e15624784eadde489ae1b026cb69c2b99521cf8c648cc63991c4
7
+ data.tar.gz: eee9ea6c5f903366017db9f2368455bad3d2e541d9b2d8ccc6d7ca31e180013888832918a647380c0bcfa62920054858181d25c42f6e6aa516b167e62585035d
@@ -0,0 +1,9 @@
1
+ # Changes
2
+
3
+ ## 0.2.0
4
+
5
+ * Add #relative_from
6
+
7
+ ## 0.1.0
8
+
9
+ * Initial release
data/README.md CHANGED
@@ -8,7 +8,66 @@ Add `rubypath` to your Gemfile, `gemspec` or install manually.
8
8
 
9
9
  ## Usage
10
10
 
11
- TODO
11
+ Using `Path` with file and directory methods:
12
+
13
+ ```
14
+ base = Path '/path/to/base'
15
+ src = base.mkpath 'project/src'
16
+ src.touch 'Rakefile'
17
+ src.mkdir('lib').mkdir('mylib').touch('version.rb')
18
+ #=> <Path '/path/to/base/project/src/lib/mylib/version.rb'
19
+ ```
20
+
21
+ Using IO:
22
+
23
+ ```
24
+ src.write "module Mylib\n VERSION = '0.1.0'\nend"
25
+
26
+ src.lookup('project.yml').read
27
+ #=> "..."
28
+ ```
29
+
30
+ ### Mock FS in tests
31
+
32
+ Wrap specific or just all specs in a virtual filesystem:
33
+
34
+ ```
35
+ # spec_helper.rb
36
+
37
+ config.around(:each) do |example|
38
+ Path::Backend.mock root: :tmp, &example
39
+ end
40
+ ```
41
+
42
+ Supported options for `:root` are `:tmp` using the real filesystem but scoping all actions into a temporary directory similar to chroot or a custom defined path to use as "chroot" directory. This mode does not allow to stub users, home directories and some attributes.
43
+
44
+ If not `:root` is specified a completely virtual in-memory filesystem will be used. This backend allows to even specify available users and home directories, the current user etc.
45
+
46
+ You can then define a specific scenario in your specs:
47
+
48
+ ```ruby
49
+ before do
50
+ Path.mock do |root, backend|
51
+ backend.cwd = '/root'
52
+ backend.current_user = 'test'
53
+ backend.homes = {'test' => '/home/test'}
54
+
55
+ home = root.mkpath('/home/test')
56
+ home.mkfile('src/test.txt').write 'CONTENT'
57
+ home.mkfile('src/test.html').write '<html><head><title></title>...'
58
+ end
59
+ end
60
+
61
+ it 'should mock all FS' do
62
+ base = Path('~test').expand
63
+ expect(base.join(%w(src test.txt)).read).to eq 'CONTENT'
64
+
65
+ files = base.glob('**/*').select{|p| p.file? }
66
+ expect(files.size).to eq 2
67
+ end
68
+ ```
69
+
70
+ See full API documentation here: http://rubydoc.info/gems/rubypath/Path
12
71
 
13
72
  ## Contributing
14
73
 
@@ -61,13 +61,13 @@
61
61
 
62
62
  <iframe id="search_frame"></iframe>
63
63
 
64
- <div id="content"><div id='filecontents'><h1>Ruby::Path</h1>
64
+ <div id="content"><div id='filecontents'><h1>Ruby Path</h1>
65
65
 
66
- <p><em>Ruby::Path</em> introduces a global <code>Path</code> class unifying most <code>File</code>, <code>Dir</code>, <code>FileUtils</code>, <code>Pathname</code> and <code>IO</code> operations with a flexible and powerful Object-Interface and still adding new useful methods and functions like mocking a while file system for fast and reliable testing.</p>
66
+ <p><em>Ruby Path</em> introduces a global <code>Path</code> class unifying most <code>File</code>, <code>Dir</code>, <code>FileUtils</code>, <code>Pathname</code> and <code>IO</code> operations with a flexible and powerful Object-Interface and still adding new useful methods and functions like mocking a while file system for fast and reliable testing.</p>
67
67
 
68
68
  <h2>Installation</h2>
69
69
 
70
- <p>Add <code>ruby-path</code> to your Gemfile, <code>gemspec</code> or install manually.</p>
70
+ <p>Add <code>rubypath</code> to your Gemfile, <code>gemspec</code> or install manually.</p>
71
71
 
72
72
  <h2>Usage</h2>
73
73
 
@@ -87,24 +87,17 @@
87
87
 
88
88
  <p>Copyright (C) 2014 Jan Graichen</p>
89
89
 
90
- <p>This program is free software: you can redistribute it and/or modify
91
- it under the terms of the GNU General Public License as published by
92
- the Free Software Foundation, either version 3 of the License, or
93
- (at your option) any later version.</p>
90
+ <p>This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.</p>
94
91
 
95
- <p>This program is distributed in the hope that it will be useful,
96
- but WITHOUT ANY WARRANTY; without even the implied warranty of
97
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
98
- GNU General Public License for more details.</p>
92
+ <p>This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.</p>
99
93
 
100
- <p>You should have received a copy of the GNU General Public License
101
- along with this program. If not, see <a href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>.</p>
94
+ <p>You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <a href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>.</p>
102
95
  </div></div>
103
96
 
104
97
  <div id="footer">
105
- Generated on Mon Feb 17 00:33:05 2014 by
98
+ Generated on Wed Apr 16 20:25:34 2014 by
106
99
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
107
- 0.8.7.3 (ruby-2.1.0).
100
+ 0.8.7.3 (ruby-2.1.1).
108
101
  </div>
109
102
 
110
103
  </body>
@@ -64,8 +64,8 @@ class Path
64
64
  end
65
65
 
66
66
  #
67
- def glob(pattern, flags = ::File::FNM_EXTGLOB)
68
- Path.glob ::File.join(escaped_glob_path, pattern), flags
67
+ def glob(pattern, flags = ::File::FNM_EXTGLOB, &block)
68
+ Path.glob(::File.join(escaped_glob_path, pattern), flags, &block)
69
69
  end
70
70
 
71
71
  private
@@ -250,4 +250,39 @@ class Path
250
250
  self
251
251
  end
252
252
  end
253
+
254
+ # Return a relative path from the given base path to the receiver path.
255
+ #
256
+ # Both paths need to be either absolute or relative otherwise an error
257
+ # will be raised. The file system will not be accessed and no symlinks are
258
+ # assumed.
259
+ #
260
+ # @example
261
+ # relative = Path('src/lib/module1/class.rb')
262
+ # .relative_from('src/lib/module2')
263
+ # #=> <Path '../module1/class.rb'>
264
+ #
265
+ # @return [Path] Relative path from argument to receiver.
266
+ # @see Pathname#relative_path_from
267
+ #
268
+ def relative_from(base)
269
+ base, path = Path(base).cleanpath, cleanpath
270
+
271
+ if (base.relative? && path.absolute?) || (base.absolute? && path.relative?)
272
+ raise ArgumentError.new \
273
+ "Different prefix: #{base.inspect} and #{path.inspect}"
274
+ end
275
+
276
+ base, path = base.components, path.components
277
+ base.shift && path.shift while base.first == path.first
278
+
279
+ Path(*((['..'] * base.size) + path))
280
+ end
281
+ alias_method :relative_path_from, :relative_from
282
+
283
+ protected
284
+
285
+ def cleanpath
286
+ Path Pathname.new(self).cleanpath
287
+ end
253
288
  end
@@ -1,7 +1,7 @@
1
1
  class Path
2
2
  module VERSION
3
3
  MAJOR = 0
4
- MINOR = 1
4
+ MINOR = 2
5
5
  PATCH = 0
6
6
  STAGE = nil
7
7
  STRING = [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join('.').freeze
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'README examples' do
4
+ describe 'Mock FS' do
5
+ around {|example| Path::Backend.mock(&example) }
6
+
7
+ before do
8
+ Path.mock do |root, backend|
9
+ backend.cwd = '/root'
10
+ backend.current_user = 'test'
11
+ backend.homes = {'test' => '/home/test'}
12
+
13
+ home = root.mkpath('/home/test')
14
+ home.mkfile('src/test.txt').write 'CONTENT'
15
+ home.mkfile('src/test.html').write '<html><head><title></title>...'
16
+ end
17
+ end
18
+
19
+ it 'should mock all FS' do
20
+ base = Path('~test').expand
21
+ expect(base.join(%w(src test.txt)).read).to eq 'CONTENT'
22
+
23
+ files = base.glob('**/*').select{|p| p.file? }
24
+ expect(files.size).to eq 2
25
+ end
26
+ end
27
+ end
@@ -269,6 +269,39 @@ describe Path do
269
269
  end
270
270
  end
271
271
 
272
+ describe_method :relative_from, aliases: [:relative_path_from] do
273
+ let(:base) { Path '/path/three/four/five' }
274
+ let(:path) { Path '/path/one/two' }
275
+ subject { path.relative_from base }
276
+
277
+ it { should eq '../../../one/two' }
278
+
279
+ context 'with collapsing paths' do
280
+ let(:base) { Path '/path/one/two' }
281
+ let(:path) { Path '/path/one' }
282
+ it { should eq '..' }
283
+ end
284
+
285
+ context 'with relative paths' do
286
+ let(:base) { Path 'path/one' }
287
+ let(:path) { Path 'path/two' }
288
+ it { should eq '../two' }
289
+ end
290
+
291
+ context 'with mixed paths' do
292
+ let(:base) { Path '/root/path/one' }
293
+ let(:path) { Path 'path/two' }
294
+ subject { ->{ path.relative_from base } }
295
+ it { should raise_error ArgumentError }
296
+ end
297
+
298
+ context 'with dots in path' do
299
+ let(:base) { Path '/path/one/three/../two' }
300
+ let(:path) { Path '/path/one/two/six' }
301
+ it { should eq 'six' }
302
+ end
303
+ end
304
+
272
305
  describe_method :ascend, aliases: [:each_ancestors] do
273
306
  shared_examples 'ascend' do
274
307
  context 'with block' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubypath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-05 00:00:00.000000000 Z
11
+ date: 2014-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -32,6 +32,7 @@ executables: []
32
32
  extensions: []
33
33
  extra_rdoc_files: []
34
34
  files:
35
+ - CHANGELOG.md
35
36
  - LICENSE.txt
36
37
  - README.md
37
38
  - doc/file.README.html
@@ -52,6 +53,7 @@ files:
52
53
  - lib/rubypath/path_predicates.rb
53
54
  - lib/rubypath/version.rb
54
55
  - rubypath.gemspec
56
+ - spec/README_spec.rb
55
57
  - spec/rubypath/comparison_spec.rb
56
58
  - spec/rubypath/construction_spec.rb
57
59
  - spec/rubypath/dir_operations_spec.rb
@@ -91,6 +93,7 @@ specification_version: 4
91
93
  summary: Path library incorporating File, Dir, Pathname, IO methods as well as a virtual
92
94
  mock filesystem.
93
95
  test_files:
96
+ - spec/README_spec.rb
94
97
  - spec/rubypath/comparison_spec.rb
95
98
  - spec/rubypath/construction_spec.rb
96
99
  - spec/rubypath/dir_operations_spec.rb