filesystem_cleaner 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +102 -0
- data/Rakefile +1 -0
- data/filesystem_cleaner.gemspec +59 -0
- data/lib/filesystem_cleaner.rb +40 -0
- data/lib/filesystem_cleaner/version.rb +3 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c89edfaacfe07d71c9bbb6fbfd87c2dfea1125be
|
4
|
+
data.tar.gz: c453a5267031e7c0e5716006dbb7cd59f4c954fe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5673aed62a26c418fec4051e20c3840aa70bc34a3988b7bbb8d9e02e12179a5fc79d0c7089e14ff7e25d1062dc5918ebf9155aaabd9f58689450231766cba1a1
|
7
|
+
data.tar.gz: e95e818143a8996b2aaf2929ff114c000f35bc03a35e7e9a0983e1e093bcde14c6bc992b84034abbc85c1ce7f17b7c3362953cb0236a3027dc893a89d27945e7
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Mark Lorenz
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# FilesystemCleaner
|
2
|
+
|
3
|
+
Like [database_cleaner](https://github.com/DatabaseCleaner/database_cleaner), but for tests that do file manipulation.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile and bundle:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
group :test do
|
11
|
+
gem 'filesystem_cleaner'
|
12
|
+
end
|
13
|
+
```
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
```ruby
|
17
|
+
# spec/spec_helper.rb
|
18
|
+
|
19
|
+
RSpec.configure do |config|
|
20
|
+
fs_cleaner = FileSystemCleaner.new(
|
21
|
+
Rails.root.join("public", "system", "test", "files"),
|
22
|
+
Rails.root.join("tmp, "files.bak")
|
23
|
+
)
|
24
|
+
|
25
|
+
config.before(:each) { fs_cleaner.save_files!(example) }
|
26
|
+
config.after(:each) { fs_cleaner.restore_files!(example) }
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
# spec/some_spec.rb
|
32
|
+
|
33
|
+
describe "a filesystem modification", file: true do # `file: true` activates the cleaner
|
34
|
+
before do
|
35
|
+
test_file = Rails.root.join("public", "system", "test", "files", "#{filename}.txt")
|
36
|
+
`cp #{fixture_file} #{test_file}`
|
37
|
+
end
|
38
|
+
|
39
|
+
it "reads the file" do
|
40
|
+
expect(MyFileReader.new(filename).to_s).to eq(File.read(fixture_file))
|
41
|
+
end
|
42
|
+
|
43
|
+
let(:filename) { SecureRandom.uuid }
|
44
|
+
let(:fixture_file) { Rails.root.join("spec", "fixtures", "some_data.txt") }
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
After the test runs, `public/system/test/files` is restored to it's pre-test state.
|
49
|
+
|
50
|
+
## Bonus
|
51
|
+
|
52
|
+
This has nothing to do with filesystem cleaner, but is a neat trick if you have to look at the files after the test to know if they were modified correctly (e.g. pdfs)
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
describe "applying a signature", file: true do
|
56
|
+
|
57
|
+
before do
|
58
|
+
`cp #{Settings.fixture_drawing} #{working_file}`
|
59
|
+
post "/documents/#{document.id}/sign"
|
60
|
+
end
|
61
|
+
|
62
|
+
it "applies the engineer's signature" do
|
63
|
+
# `quality_assurance_dir` is _not_ cleaned by filesystem_cleaner
|
64
|
+
`cp #{working_file} #{Settings.quality_assurance_dir}`
|
65
|
+
|
66
|
+
fname = filename
|
67
|
+
RSpec.configure do |config|
|
68
|
+
config.after(:suite) do
|
69
|
+
qa_file_path = "#{Settings.quality_assurance_dir}/#{fname}"
|
70
|
+
message = "ssh me@my-host cat #{qa_file_path} | display - "
|
71
|
+
puts
|
72
|
+
puts
|
73
|
+
print message
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
Produces a nice output:
|
82
|
+
```
|
83
|
+
$ bundle exec rspec
|
84
|
+
............*.......
|
85
|
+
|
86
|
+
ssh me@my-host cat /home/me/app/spec/support/qa/b5f69a0-aa53-c39e0c27e2 | display -
|
87
|
+
|
88
|
+
Pending:
|
89
|
+
Some spec is pending for some reason or other
|
90
|
+
# No reason given
|
91
|
+
# ./spec/mailers/mailer_spec.rb:8
|
92
|
+
```
|
93
|
+
|
94
|
+
Now you can just copy and paste that ssh command to view the file locally.
|
95
|
+
|
96
|
+
## Contributing
|
97
|
+
|
98
|
+
1. Fork it
|
99
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
100
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
101
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
102
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'filesystem_cleaner/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "filesystem_cleaner"
|
8
|
+
spec.version = FilesystemCleaner::VERSION
|
9
|
+
spec.authors = ["Mark Lorenz"]
|
10
|
+
spec.email = ["markjlorenz@dapplebeforedawn.com"]
|
11
|
+
spec.description = %q{For tests that need to modify files}
|
12
|
+
spec.summary = <<-SUMMARY
|
13
|
+
```ruby
|
14
|
+
# spec/spec_helper.rb
|
15
|
+
|
16
|
+
RSpec.configure do |config|
|
17
|
+
fs_cleaner = FileSystemCleaner.new(
|
18
|
+
Rails.root.join("public", "system", "test", "files"),
|
19
|
+
Rails.root.join("tmp, "files.bak")
|
20
|
+
)
|
21
|
+
|
22
|
+
config.before(:each) { fs_cleaner.save_files!(example) }
|
23
|
+
config.after(:each) { fs_cleaner.restore_files!(example) }
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# spec/some_spec.rb
|
29
|
+
|
30
|
+
describe "a filesystem modification", file: true do # `file: true` activates the cleaner
|
31
|
+
before do
|
32
|
+
test_file = Rails.root.join("public", "system", "test", "files", "\#{filename}.txt")
|
33
|
+
`cp \#{fixture_file} \#{test_file}`
|
34
|
+
end
|
35
|
+
|
36
|
+
it "reads the file" do
|
37
|
+
expect(MyFileReader.new(filename).to_s).to eq(File.read(fixture_file))
|
38
|
+
end
|
39
|
+
|
40
|
+
let(:filename) { SecureRandom.uuid }
|
41
|
+
let(:fixture_file) { Rails.root.join("spec", "fixtures", "some_data.txt") }
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
After the test runs, `public/system/test/files` is restored to it's pre-test state.
|
46
|
+
SUMMARY
|
47
|
+
spec.homepage = "https://github.com/dapplebeforedawn/filesystem-cleaner-gem"
|
48
|
+
spec.license = "MIT"
|
49
|
+
|
50
|
+
spec.files = `git ls-files`.split($/)
|
51
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
52
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
53
|
+
spec.require_paths = ["lib"]
|
54
|
+
|
55
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
56
|
+
spec.add_development_dependency "rake"
|
57
|
+
|
58
|
+
spec.add_dependency "rspec", "~>2.0"
|
59
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "filesystem_cleaner/version"
|
2
|
+
|
3
|
+
# Usage:
|
4
|
+
#
|
5
|
+
# RSpec.configure do |config|
|
6
|
+
# fs_cleaner = FileSystemCleaner.new(
|
7
|
+
# Rails.root.join("public", "system", "test", "files"),
|
8
|
+
# Rails.root.join("tmp", "files.bak")
|
9
|
+
# )
|
10
|
+
#
|
11
|
+
# config.before(:each) { fs_cleaner.save_files!(example) }
|
12
|
+
# config.after(:each) { fs_cleaner.restore_files!(example) }
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
class FileSystemCleaner
|
16
|
+
|
17
|
+
attr_reader :filesystem_under_test, :backup_location
|
18
|
+
|
19
|
+
def initialize filesystem_under_test, backup_location
|
20
|
+
@filesystem_under_test = filesystem_under_test
|
21
|
+
@backup_location = backup_location
|
22
|
+
end
|
23
|
+
|
24
|
+
def save_files!(example)
|
25
|
+
return unless should_perform?(example)
|
26
|
+
`rsync -a #{filesystem_under_test}/ #{backup_location}`
|
27
|
+
end
|
28
|
+
|
29
|
+
def restore_files!(example)
|
30
|
+
return unless should_perform?(example)
|
31
|
+
`rsync -a --delete #{backup_location}/ #{filesystem_under_test}/`
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def should_perform?(example)
|
37
|
+
example.metadata[:file]
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: filesystem_cleaner
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mark Lorenz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
55
|
+
description: For tests that need to modify files
|
56
|
+
email:
|
57
|
+
- markjlorenz@dapplebeforedawn.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- filesystem_cleaner.gemspec
|
68
|
+
- lib/filesystem_cleaner.rb
|
69
|
+
- lib/filesystem_cleaner/version.rb
|
70
|
+
homepage: https://github.com/dapplebeforedawn/filesystem-cleaner-gem
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.2.2
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: '```ruby # spec/spec_helper.rb RSpec.configure do |config| fs_cleaner =
|
94
|
+
FileSystemCleaner.new( Rails.root.join("public", "system", "test", "files"), Rails.root.join("tmp,
|
95
|
+
"files.bak") ) config.before(:each) { fs_cleaner.save_files!(example) } config.after(:each) {
|
96
|
+
fs_cleaner.restore_files!(example) } end ``` ```ruby # spec/some_spec.rb describe
|
97
|
+
"a filesystem modification", file: true do # `file: true` activates the cleaner
|
98
|
+
before do test_file = Rails.root.join("public", "system", "test", "files", "#{filename}.txt")
|
99
|
+
`cp #{fixture_file} #{test_file}` end it "reads the file" do expect(MyFileReader.new(filename).to_s).to
|
100
|
+
eq(File.read(fixture_file)) end let(:filename) { SecureRandom.uuid } let(:fixture_file)
|
101
|
+
{ Rails.root.join("spec", "fixtures", "some_data.txt") } end ``` After the test
|
102
|
+
runs, `public/system/test/files` is restored to it''s pre-test state.'
|
103
|
+
test_files: []
|