Sha256: def44efd7adb5a982bf547fa6627df3097445e385b0aa5773f64f7e14c1a0700

Contents?: true

Size: 1.93 KB

Versions: 14

Compression:

Stored size: 1.93 KB

Contents

require 'rspec/expectations/version'

# @!method have_permissions(permissions)
#   This matchers checks if <file> or <directory> has <perm> permissions
#
#   @param [Fixnum, String] permissions
#     The permissions as octal number, e.g. `0700`, or String, e.g. `'0700'`
#
#   @return [TrueClass, FalseClass] The result
#
#     false:
#     * if file has permissions
#     true:
#     * if file does not have permissions
#
#   @example Use matcher with octal number
#
#     RSpec.describe do
#       it { expect(file).to have_permissions(0700) }
#       it { expect(directory).to have_permissions(0700) }
#     end
#
#   @example Use matcher with string
#
#     RSpec.describe do
#       it { expect(file).to have_permissions('0700') }
#       it { expect(files).to include a_path_with_permissions('0700') }
#       it { expect(directory).to have_permissions('0700') }
#       it { expect(directories).to include a_path_with_permissions('0700') }
#     end
RSpec::Matchers.define :have_permissions do |expected|
  def permissions(file)
    @actual = format("%o", File::Stat.new(file).mode)[-4,4].gsub(/^0*/, '')
  end

  match do |actual|
    stop_all_commands

    @old_actual = actual
    @actual = permissions(expand_path(@old_actual))

    @expected = if expected.is_a? Integer
                  expected.to_s(8)
                elsif expected.is_a? String
                  expected.gsub(/^0*/, '')
                else
                  expected
                end

    values_match? @expected, @actual
  end

  failure_message do |actual|
    format("expected that path \"%s\" has permissions \"%s\", but has \"%s\".", @old_actual, @expected, @actual)
  end

  failure_message_when_negated do |actual|
    format("expected that path \"%s\" does not have permissions \"%s\", but has \"%s\".", @old_actual, @expected, @actual)
  end
end

if RSpec::Expectations::Version::STRING >= '3.0'
  RSpec::Matchers.alias_matcher :a_path_having_permissions, :have_permissions
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
aruba-0.12.0 lib/aruba/matchers/path/have_permissions.rb
aruba-0.11.2 lib/aruba/matchers/path/have_permissions.rb
aruba-0.11.1 lib/aruba/matchers/path/have_permissions.rb
aruba-0.11.0.pre4 lib/aruba/matchers/path/have_permissions.rb
aruba-0.11.0.pre3 lib/aruba/matchers/path/have_permissions.rb
aruba-0.11.0.pre2 lib/aruba/matchers/path/have_permissions.rb
aruba-0.11.0.pre lib/aruba/matchers/path/have_permissions.rb
aruba-0.10.2 lib/aruba/matchers/path/have_permissions.rb
aruba-0.10.1 lib/aruba/matchers/path/have_permissions.rb
aruba-0.10.0 lib/aruba/matchers/path/have_permissions.rb
aruba-0.10.0.pre2 lib/aruba/matchers/path/have_permissions.rb
aruba-0.10.0.pre lib/aruba/matchers/path/have_permissions.rb
aruba-0.9.0 lib/aruba/matchers/path/have_permissions.rb
aruba-0.9.0.pre2 lib/aruba/matchers/path/have_permissions.rb