https://github.com/jgraichen/rubypath
Path library incorporating File, Dir, Pathname, IO methods as well as a virtual mock filesystem.
https://github.com/jgraichen/rubypath
Last synced: about 1 year ago
JSON representation
Path library incorporating File, Dir, Pathname, IO methods as well as a virtual mock filesystem.
- Host: GitHub
- URL: https://github.com/jgraichen/rubypath
- Owner: jgraichen
- License: lgpl-3.0
- Created: 2014-04-16T18:36:41.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2022-02-25T21:17:20.000Z (over 4 years ago)
- Last Synced: 2024-05-01T21:22:10.708Z (about 2 years ago)
- Language: Ruby
- Size: 115 KB
- Stars: 3
- Watchers: 5
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Ruby Path
[](http://badge.fury.io/rb/rubypath)
[](https://travis-ci.org/jgraichen/rubypath)
[](https://coveralls.io/r/jgraichen/rubypath)
[](https://gemnasium.com/jgraichen/rubypath)
[](http://rubydoc.info/github/jgraichen/rubypath/master/frames)
*Ruby Path* introduces a global `Path` class unifying most `File`, `Dir`, `FileUtils`, `Pathname` and `IO` operations with a flexible and powerful Object-Interface and still adding new useful methods and functions like mocking a whole file system for fast and reliable testing.
## Installation
Add `rubypath` to your Gemfile, `gemspec` or install manually.
## Usage
Using `Path` with file and directory methods:
```ruby
base = Path '/path/to/base'
src = base.mkpath 'project/src'
src.touch 'Rakefile'
src.mkdir('lib').mkdir('mylib').touch('version.rb')
#=> "..."
```
### Mock FS in tests
Wrap specific or just all specs in a virtual filesystem:
```ruby
# spec_helper.rb
config.around(:each) do |example|
Path::Backend.mock root: :tmp, &example
end
```
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.
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.
You can then define a specific scenario in your specs:
```ruby
before do
Path.mock do |root, backend|
backend.cwd = '/root'
backend.current_user = 'test'
backend.homes = {'test' => '/home/test'}
home = root.mkpath('/home/test')
home.mkfile('src/test.txt').write 'CONTENT'
home.mkfile('src/test.html').write '...'
end
end
it 'should mock all FS' do
base = Path('~test').expand
expect(base.join(%w(src test.txt)).read).to eq 'CONTENT'
files = base.glob('**/*').select{|p| p.file? }
expect(files.size).to eq 2
end
```
See full API documentation here: http://rubydoc.info/gems/rubypath/Path
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Add specs testing SYS *and* MOCK file system
4. Commit your specs (`git commit -am 'Add specs for feature'`)
5. Add our changes for SYS *and* MOCK file system
6. Commit your changes (`git commit -am 'Add some feature'`)
7. Push to the branch (`git push origin my-new-feature`)
8. Create new Pull Request
### ToDos
* Add missing methods
* Improve MOCK FS implementation
## License
Copyright (C) 2014 Jan Graichen
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.
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.
You should have received a copy of the GNU Lesser General Public License along with this program. If not, see .