LoginSignup
1
1

More than 5 years have passed since last update.

Aruba gem - Filesystem

Posted at

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

サンプル

cd

ディレクトリの移動をする

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
  • 実行結果
$ 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

directory?

ディレクトリかどうか判定する

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
  • 実行結果
$ 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

file?

fileかどうか判定する

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
  • 実行結果
$ 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

exist?

ファイル/ディレクトリが存在するか確認する

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
  • 実行結果
$ 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

absolute?

絶対パスかどうか確認する

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
  • 実行結果
$ 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

relative?

相対パスかどうか確認する

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
  • 実行結果
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

create_directory

ディレクトリを作成する

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
  • 実行結果
$ 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

move

ファイル/ディレクトリを移動する

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
  • 実行結果
$ 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

外部資料

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1