Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fixtures to aruba #224

Merged
merged 3 commits into from Apr 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,69 @@ end

Refer to http://blog.headius.com/2010/03/jruby-startup-time-tips.html for other tips on startup time.

## Fixtures

Sometimes your tests need existing files to work - e.g binary data files you
cannot create programmatically. Since `aruba` >= 0.6.3 includes some basic
support for fixtures. All you need to do is the following:

1. Create a `fixtures`-directory
2. Create fixture files in this directory

The `expand_path`-helper will expand `%` to the path of your fixtures
directory:

```ruby
expand_path('%/song.mp3')
# => /home/user/projects/my_project/fixtures/song.mp3
```

*Example*

1. Create fixtures directory

```bash
cd project

mkdir -p fixtures/
# or
mkdir -p test/fixtures/
# or
mkdir -p spec/fixtures/
# or
mkdir -p features/fixtures/
```

2. Store `song.mp3` in `fixtures`-directory

```bash
cp song.mp3 fixtures/
```

3. Add fixture to vcs-repository - e.g. `git`, `mercurial`

4. Create test

```ruby
RSpec.describe 'My Feature' do
describe '#read_music_file' do
context 'when the file exists' do
let(:path) { expand_path('%/song.mp3') }

before :each do
in_current_directory { FileUtils.cp path, 'file.mp3' }
end

before :each do
run 'my_command'
end

it { expect(all_stdout).to include('Rate is 128 KB') }
end
end
end
```

## Reporting

*Important* - you need [Pygments](http://pygments.org/) installed to use this feature.
Expand Down
66 changes: 57 additions & 9 deletions lib/aruba/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'rspec/expectations'
require 'aruba'
require 'aruba/config'
require 'ostruct'
require 'pathname'

Dir.glob( File.join( File.expand_path( '../matchers' , __FILE__ ) , '*.rb' ) ).each { |rb| require rb }

Expand All @@ -21,20 +23,32 @@ module Api
# @example Single file name
#
# # => <path>/tmp/aruba/file
# absolute_path('file')
# expand_path('file')
#
# @example Single Dot
#
# # => <path>/tmp/aruba
# absolute_path('.')
# expand_path('.')
#
# @example Join and Expand path
#
# # => <path>/tmp/aruba/path/file
# absolute_path('path', 'file')
# expand_path('path', 'file')
#
def expand_path(*args)
if %r{^#{fixtures_path_prefix}/} === args.first
File.join fixtures_directory, args.shift.sub(%r{^#{fixtures_path_prefix}/}, ''), *args
else
in_current_directory { File.expand_path File.join(*args) }
end
end

# @private
# @deprecated
def absolute_path(*args)
in_current_directory { File.expand_path File.join(*args) }
warn('The use of "absolute_path" is deprecated. Use "expand_path" instead')

expand_path(*args)
end

# Execute block in current directory
Expand Down Expand Up @@ -126,7 +140,7 @@ def touch_file(*args)
{}
end

args = args.map { |p| absolute_path(p) }
args = args.map { |p| expand_path(p) }
args.each { |p| _mkdir(File.dirname(p)) }

FileUtils.touch(args, options)
Expand Down Expand Up @@ -194,7 +208,7 @@ def filesystem_permissions(*args)
mode
end

args = args.map { |p| absolute_path(p) }
args = args.map { |p| expand_path(p) }
args.each { |p| raise "Expected #{p} to be present" unless FileTest.exists?(p) }

FileUtils.chmod(mode, args, options)
Expand Down Expand Up @@ -224,7 +238,7 @@ def check_filesystem_permissions(*args)
expected_permissions = args.shift
expected_result = args.pop

args = args.map { |p| absolute_path(p) }
args = args.map { |p| expand_path(p) }

args.each do |p|
raise "Expected #{p} to be present" unless FileTest.exists?(p)
Expand Down Expand Up @@ -269,7 +283,7 @@ def remove_file(*args)
{}
end

args = args.map { |p| absolute_path(p) }
args = args.map { |p| expand_path(p) }

FileUtils.rm(args, options)
end
Expand Down Expand Up @@ -322,7 +336,7 @@ def remove_directory(*args)
{}
end

args = args.map { |p| absolute_path(p) }
args = args.map { |p| expand_path(p) }

FileUtils.rm_r(args, options)
end
Expand Down Expand Up @@ -788,6 +802,40 @@ def io_wait
@aruba_io_wait_seconds || DEFAULT_IO_WAIT_SECONDS
end

DEFAULT_ROOT_DIRECTORY = Dir.getwd

# The root directory of aruba
def root_directory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making aruba root directory configurable is not related to this PR (as I see it). I suggest you take that out in a seperate PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, but it makes the configuration of the fixtures more cleaner. I added a new PR for this. Would you mind to merge it first, then?

@aruba_root_directory ||= DEFAULT_ROOT_DIRECTORY
end

# Path prefix for fixtures
def fixtures_path_prefix
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making the fixture prefix character configurable via @aruba_fixture_path_prefix. To be honest I think it should not be configurable; just like ~ referring to HOME directory is not configurable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. changed.

'%'
end

DEFAULT_FIXTURES_DIRECTORY = 'fixtures'

# The path to the directory which contains fixtures
# You might want to overwrite this method to place your data else where.
#
# @return [String]
# The directory to where your fixtures are stored
def fixtures_directory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the finding of the fixture directory can be implemented such that it is only executed once only. I.e. Setting a class variable straight in the class body. And making it configurable like #exit_timeout and #io_wait Does it make sense?

return @fixtures_directory if @fixtures_directory

default_path = proc { File.join(root_directory, DEFAULT_FIXTURES_DIRECTORY) }

candidates = %w(
fixtures
spec/fixtures
features/fixtures
test/fixtures
).map { |dir| File.join(root_directory, dir) }

@fixtures_directory = candidates.find(default_path) { |dir| File.directory? dir }
end

# Run a command with aruba
#
# Checks for error during command execution and checks the output to detect
Expand Down
4 changes: 2 additions & 2 deletions lib/aruba/matchers/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
RSpec::Matchers.define :have_same_file_content_like do |expected|
match do |actual|
FileUtils.compare_file(
absolute_path(actual),
absolute_path(expected)
expand_path(actual),
expand_path(expected)
)
end

Expand Down
6 changes: 3 additions & 3 deletions lib/aruba/matchers/mode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

match do |actual|
file_name = actual
actual_permissions = sprintf( "%o", File::Stat.new(absolute_path(file_name)).mode )[-4,4].gsub(/^0*/, '')
actual_permissions = sprintf( "%o", File::Stat.new(expand_path(file_name)).mode )[-4,4].gsub(/^0*/, '')

actual_permissions == expected_permissions
end

failure_message do |actual|
file_name = actual
actual_permissions = sprintf( "%o", File::Stat.new(absolute_path(file_name)).mode )[-4,4].gsub(/^0*/, '')
actual_permissions = sprintf( "%o", File::Stat.new(expand_path(file_name)).mode )[-4,4].gsub(/^0*/, '')

format("expected that file \"%s\" would have permissions \"%s\", but has \"%s\".", file_name, expected_permissions, actual_permissions)
end

failure_message_when_negated do |actual|
file_name = actual
actual_permissions = sprintf( "%o", File::Stat.new(absolute_path(file_name)).mode )[-4,4].gsub(/^0*/, '')
actual_permissions = sprintf( "%o", File::Stat.new(expand_path(file_name)).mode )[-4,4].gsub(/^0*/, '')

format("expected that file \"%s\" would not have permissions \"%s\", but has \"%s\".", file_name, expected_permissions, actual_permissions)
end
Expand Down
74 changes: 67 additions & 7 deletions spec/aruba/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def announce_or_puts(*args)
@directory_path = File.join(@aruba.current_directory, @directory_name)
end

context '#create_dir' do
context '#create_directory' do
it 'creates a directory' do
@aruba.create_dir @directory_name
@aruba.create_directory @directory_name
expect(File.exist?(File.expand_path(@directory_path))).to be_truthy
end
end
Expand Down Expand Up @@ -171,21 +171,37 @@ def announce_or_puts(*args)
end
end

context '#absolute_path' do
context '#expand_path' do
it 'expands and returns path' do
expect(@aruba.absolute_path(@file_name)).to eq File.expand_path(@file_path)
expect(@aruba.expand_path(@file_name)).to eq File.expand_path(@file_path)
end

it 'removes "."' do
expect(@aruba.absolute_path('.')).to eq File.expand_path(current_directory)
expect(@aruba.expand_path('.')).to eq File.expand_path(current_directory)
end

it 'removes ".."' do
expect(@aruba.absolute_path('..')).to eq File.expand_path(File.join(current_directory, '..'))
expect(@aruba.expand_path('..')).to eq File.expand_path(File.join(current_directory, '..'))
end

it 'joins multiple arguments' do
expect(@aruba.absolute_path('path', @file_name)).to eq File.expand_path(File.join(current_directory, 'path', @file_name))
expect(@aruba.expand_path('path', @file_name)).to eq File.expand_path(File.join(current_directory, 'path', @file_name))
end

it 'resolves fixtures path' do
klass = Class.new do
include Aruba::Api

def root_directory
File.expand_path(current_directory)
end
end

aruba = klass.new

aruba.touch_file 'fixtures/file1'

expect(aruba.expand_path('%/file1')).to eq File.expand_path(File.join(current_directory, 'fixtures', 'file1'))
end
end

Expand Down Expand Up @@ -665,6 +681,50 @@ def actuctual_permissions
end
end

describe 'fixtures' do
let(:aruba) do
klass = Class.new do
include Aruba::Api

def root_directory
expand_path('.')
end
end

klass.new
end

describe '#fixtures_directory' do
context 'when "/fixtures"-directory exist' do
before(:each) { aruba.create_directory('fixtures') }

it { expect(aruba.fixtures_directory).to eq expand_path('fixtures') }
end

context 'when "/features/fixtures"-directory exist' do
before(:each) { aruba.create_directory('features/fixtures') }

it { expect(aruba.fixtures_directory).to eq expand_path('features/fixtures') }
end

context 'when "/spec/fixtures"-directory exist' do
before(:each) { aruba.create_directory('spec/fixtures') }

it { expect(aruba.fixtures_directory).to eq expand_path('spec/fixtures') }
end

context 'when "/test/fixtures"-directory exist' do
before(:each) { aruba.create_directory('test/fixtures') }

it { expect(aruba.fixtures_directory).to eq expand_path('test/fixtures') }
end
end

context '#fixture_path_prefix' do
it { expect(@aruba.fixtures_path_prefix).to eq '%' }
end
end

describe "#set_env" do
after(:each) do
@aruba.stop_processes!
Expand Down