Module: TTYtest::FileAssertions

Included in:
Assertions
Defined in:
lib/ttytest/assertions/file_assertions.rb

Overview

File Assertions for ttytest2.

Instance Method Summary collapse

Instance Method Details

#assert_file_contains(file_path, needle) ⇒ Object Also known as: assert_file_like

Asserts the specified file contains the passed in string value

Parameters:

  • file_path (String)

    the path to the file

  • needle (String)

    the value to search for in the file

Raises:

  • (MatchError)

    if the file does not contain value in variable needle



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ttytest/assertions/file_assertions.rb', line 28

def assert_file_contains(file_path, needle)
  raise file_not_found_error(file_path) unless File.exist?(file_path)
  raise file_is_dir_error(file_path) unless File.file?(file_path)

  file_contains = false
  File.foreach(file_path) do |line|
    if line.include?(needle)
      file_contains = true
      break
    end
  end
  return if file_contains

  raise MatchError,
        "File with path #{file_path} did not contain #{needle}.\nEntire screen:\n#{self}"
end

#assert_file_doesnt_exist(file_path) ⇒ Object

Asserts the specified file does not exists

Parameters:

  • file_path (String)

    the path to the file

Raises:

  • (MatchError)

    if the file is found or is a directory/symlink



17
18
19
20
21
22
# File 'lib/ttytest/assertions/file_assertions.rb', line 17

def assert_file_doesnt_exist(file_path)
  return unless File.exist?(file_path) || File.file?(file_path)

  raise MatchError,
        "File with path #{file_path} was found or is a directory when it was asserted it did not exist.\nEntire screen:\n#{self}"
end

#assert_file_exists(file_path) ⇒ Object

Asserts the specified file exists

Parameters:

  • file_path (String)

    the path to the file

Raises:

  • (MatchError)

    if the file is not found or is a directory/symlink



9
10
11
12
# File 'lib/ttytest/assertions/file_assertions.rb', line 9

def assert_file_exists(file_path)
  raise file_not_found_error(file_path) unless File.exist?(file_path)
  raise file_is_dir_error(file_path) unless File.file?(file_path)
end

#assert_file_has_line_count(file_path, expected_count) ⇒ Object

Asserts the specified file has line count specified

Parameters:

  • file_path (String)

    the path to the file

  • expected_count (Integer)

    the expected line count of the file

Raises:

  • (MatchError)

    if the file has a different line count than specified



66
67
68
69
70
71
72
73
74
75
# File 'lib/ttytest/assertions/file_assertions.rb', line 66

def assert_file_has_line_count(file_path, expected_count)
  raise file_not_found_error(file_path) unless File.exist?(file_path)
  raise file_is_dir_error(file_path) unless File.file?(file_path)

  actual_count = File.foreach(file_path).count
  return if actual_count == expected_count

  raise MatchError,
        "File had #{actual_count} lines, not #{expected_count} lines as expected.\nEntire screen:\n#{self}"
end

#assert_file_has_permissions(file_path, permissions) ⇒ Object

Asserts the specified file has the permissions specified

Parameters:

  • file_path (String)

    the path to the file

  • permissions (String)

    the expected permissions of the file (in form ‘644’ or ‘775’)

Raises:

  • (MatchError)

    if the file has different permissions than specified



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ttytest/assertions/file_assertions.rb', line 50

def assert_file_has_permissions(file_path, permissions)
  raise file_not_found_error(file_path) unless File.exist?(file_path)
  raise file_is_dir_error(file_path) unless File.file?(file_path)

  file_mode = File.stat(file_path).mode
  perms_octal = format('%o', file_mode)[-3...]
  return if perms_octal == permissions

  raise MatchError,
        "File had permissions #{perms_octal}, not #{permissions} as expected.\n Entire screen:\n#{self}"
end