Aruba gem - Filesystem

Aruba の Filesystem 周りの機能についてまとめます。

## <i class='fa fa-flask'></i> サンプル
### <i class='fa fa-check'></i> cd
ディレクトリの移動をする

```ruby
require 'spec_helper'

RSpec.describe 'cd to directory', :type => :aruba do
  before(:each) do
    create_directory 'dir'
    cd 'dir'
  end

  before(:each) { run_simple 'pwd' }

  it { expect(last_command_started.output).to include 'dir' }
end
```

* 実行結果

```sh
$ rspec -fd -c spec/filesystem/cd_spec.rb

cd to directory
  should include "dir"

Finished in 0.01123 seconds (files took 0.22464 seconds to load)
1 example, 0 failures
```

### <i class='fa fa-check'></i> directory?
ディレクトリかどうか判定する

```ruby
require 'spec_helper'

RSpec.configure do |config|
  config.include Aruba::Api
end

RSpec.describe 'directory?' do
  context 'not exist' do
    let(:directory) { 'dir' }

    it { expect(directory?(directory)).to be false }
  end
  context 'exist' do
    let(:directory) { 'dir' }

    before(:each) { create_directory(directory) }

    it { expect(directory?(directory)).to be true }
  end
end
```

* 実行結果

```sh
$ rspec -fd -c spec/filesystem/directory_q_spec.rb

directory?
  not exist
    should equal false
  exist
    should equal true

Finished in 0.00736 seconds (files took 0.22212 seconds to load)
2 examples, 0 failures
```

### <i class='fa fa-check'></i> file?
fileかどうか判定する

```ruby
require 'spec_helper'

RSpec.configure do |config|
  config.include Aruba::Api
end

RSpec.describe 'file?' do
  context 'not exist' do
    let(:file) { 'file' }

    it { expect(file?(file)).to be false }
  end
  context 'exist' do
    let(:file) { 'file' }

    before(:each) { touch(file) }

    it { expect(file?(file)).to be true }
  end
end
```

* 実行結果

```sh
$ rspec -fd -c spec/filesystem/file_q_spec.rb

file?
  not exist
    should equal false
  exist
    should equal true

Finished in 0.00773 seconds (files took 0.21635 seconds to load)
2 examples, 0 failures
```

### <i class='fa fa-check'></i> exist?
ファイル/ディレクトリが存在するか確認する

```ruby
require 'spec_helper'

RSpec.configure do |config|
  config.include Aruba::Api
end

RSpec.describe 'exist?' do
  context 'not exist' do
    let(:file) { 'file' }
    let(:directory) { 'dir' }

    it { expect(exist?(directory)).to be false }
    it { expect(exist?(file)).to be false }
  end
  context 'exist' do
    let(:file) { 'file' }
    let(:directory) { 'dir' }

    before(:each) { create_directory(directory) }
    before(:each) { touch(file) }

    it { expect(directory?(directory)).to be true }
    it { expect(file?(file)).to be true }
  end
end
```

* 実行結果

```sh
$ rspec -fd -c spec/filesystem/exist_q_spec.rb

exist?
  not exist
    should equal false
    should equal false
  exist
    should equal true
    should equal true

Finished in 0.0142 seconds (files took 0.21583 seconds to load)
4 examples, 0 failures
```

### <i class='fa fa-check'></i> absolute?
絶対パスかどうか確認する

```ruby
require 'spec_helper'

RSpec.configure do |config|
  config.include Aruba::Api
end

RSpec.describe 'absolute?' do
  context 'not absolute' do
    let(:path) { 'path.txt' }

    it { expect(absolute?(path)).to be false }
  end
  context 'absolute' do
    let(:path) { '/home' }

    it { expect(absolute?(path)).to be true }
  end
end
```

* 実行結果

```sh
$ rspec -fd -c spec/filesystem/absolute_q_spec.rb

absolute?
  not absolute
    should equal false
  absolute
    should equal true

Finished in 0.00696 seconds (files took 0.21946 seconds to load)
2 examples, 0 failures
```

### <i class='fa fa-check'></i> relative?
相対パスかどうか確認する

```ruby
require 'spec_helper'

RSpec.configure do |config|
  config.include Aruba::Api
end

RSpec.describe 'relative?' do
  context 'not relative' do
    let(:path) { '/home' }

    it { expect(relative?(path)).to be false }
  end
  context 'relative' do
    let(:path) { 'path.txt' }

    it { expect(relative?(path)).to be true }
  end
end
```

* 実行結果

```sh
rspec -fd -c spec/filesystem/relative_q_spec.rb

relative?
  not relative
    should equal false
  relative
    should equal true

Finished in 0.01014 seconds (files took 0.2736 seconds to load)
2 examples, 0 failures
```

### <i class='fa fa-check'></i> create_directory
ディレクトリを作成する

```ruby
require 'spec_helper'

RSpec.configure do |config|
  config.include Aruba::Api
end

RSpec.describe 'create_directory' do
  context 'not exist' do
    let(:directory) { 'dir' }

    it { expect(directory).not_to be_an_existing_directory }
  end
  context 'exist' do
    let(:directory) { 'dir' }

    before(:each) { create_directory(directory) }

    it { expect(directory).to be_an_existing_directory }
  end
end
```

* 実行結果

```sh
$ rspec -fd -c spec/filesystem/create_directory_spec.rb
create_directory
  not exist
    should not be an existing directory
  exist
    should be an existing directory

Finished in 0.00766 seconds (files took 0.21603 seconds to load)
2 examples, 0 failures
```

### <i class='fa fa-check'></i> move
ファイル/ディレクトリを移動する

```ruby
require 'spec_helper'

RSpec.describe 'move', :type => :aruba do
  let(:old_dir) { 'old' }
  let(:new_dir) { 'new' }

  before(:each) do
    create_directory old_dir
    create_directory new_dir
    move old_dir, new_dir
  end

  it { expect(new_dir).to be_an_existing_directory }
  it { expect(old_dir).not_to be_an_existing_directory }
end
```

* 実行結果

```sh
$ rspec -fd -c spec/filesystem/move_spec.rb

move
  should be an existing directory
  should not be an existing directory

Finished in 0.00851 seconds (files took 0.2234 seconds to load)
2 examples, 0 failures
```

## <i class="fa fa-book" style="font-size:1em;"></i> 外部資料
* <i class='fa fa-external-link'></i> [Filesystem - Aruba - docs](http://www.relishapp.com/cucumber/aruba/v/0-11-0/docs/filesystem)