Class: Hiptest::CliOptionsChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/hiptest-publisher/cli_options_checker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli_options, reporter) ⇒ CliOptionsChecker

Returns a new instance of CliOptionsChecker.



9
10
11
12
# File 'lib/hiptest-publisher/cli_options_checker.rb', line 9

def initialize(cli_options, reporter)
  @cli_options = cli_options
  @reporter = reporter
end

Instance Attribute Details

#cli_optionsObject (readonly)

Returns the value of attribute cli_options.



8
9
10
# File 'lib/hiptest-publisher/cli_options_checker.rb', line 8

def cli_options
  @cli_options
end

#reporterObject (readonly)

Returns the value of attribute reporter.



8
9
10
# File 'lib/hiptest-publisher/cli_options_checker.rb', line 8

def reporter
  @reporter
end

Instance Method Details

#check!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hiptest-publisher/cli_options_checker.rb', line 14

def check!
  check_config_file

  if cli_options.only == 'list'
    return
  end

  check_secret_token

  if push?(cli_options)
    check_push_file
  else
    check_output_directory
    check_actionwords_signature_file
    check_xml_file
    check_test_run_id
    check_language_and_only
  end
end

#check_actionwords_signature_fileObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hiptest-publisher/cli_options_checker.rb', line 91

def check_actionwords_signature_file
  if cli_options.actionwords_diff?
    actionwords_signature_file = Pathname.new(cli_options.output_directory).join("actionwords_signature.yaml")
    if actionwords_signature_file.directory?
      raise CliOptionError, "Bad Action Words signature file: the file \"#{actionwords_signature_file.realpath}\" is a directory"
    elsif !actionwords_signature_file.exist?
      full_path = File.expand_path(cli_options.output_directory)
      raise CliOptionError, [
        "Missing Action Words signature file: the file \"actionwords_signature.yaml\" could not be found in directory \"#{full_path}\"",
        "Use --actionwords-signature to generate the file \"#{full_path}/actionwords_signature.yaml\"",
      ].join("\n")
    end
  end
end

#check_config_fileObject



34
35
36
37
38
39
40
# File 'lib/hiptest-publisher/cli_options_checker.rb', line 34

def check_config_file
  begin
    ParseConfig.new(cli_options.config) if present?(cli_options.config)
  rescue Errno::EACCES => err
    raise CliOptionError, "Error with --config: the file \"#{cli_options.config}\" does not exist or is not readable"
  end
end

#check_language_and_onlyObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/hiptest-publisher/cli_options_checker.rb', line 122

def check_language_and_only
  if present?(cli_options.language)
    begin
      language_config_parser = LanguageConfigParser.new(cli_options)
    rescue ArgumentError => err
      raise CliOptionError, err.message
    end

    if present?(cli_options.only)
      if language_config_parser.filtered_group_names != cli_options.groups_to_keep
        unknown_categories = cli_options.groups_to_keep - language_config_parser.group_names
        if unknown_categories.length == 1
          message = "the category #{formatted_categories(unknown_categories)} does not exist"
        else
          message = "the categories #{formatted_categories(unknown_categories)} do not exist"
        end
        raise CliOptionError, "Error with --only: #{message} for language #{cli_options.language_framework}. " +
          "Available categories are #{formatted_categories(language_config_parser.group_names)}."
      end
    end
  end
end

#check_output_directoryObject



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/hiptest-publisher/cli_options_checker.rb', line 77

def check_output_directory
  parent = first_existing_parent(cli_options.output_directory)
  if !parent.writable?
    if parent.realpath === Pathname.new(cli_options.output_directory).cleanpath
      raise CliOptionError, "Error with --output-directory: the directory \"#{@cli_options.output_directory}\" is not writable"
    else
      raise CliOptionError, "Error with --output-directory: the directory \"#{@cli_options.output_directory}\" can not be created because \"#{parent.realpath}\" is not writable"
    end
  elsif !parent.directory?
    raise CliOptionError, "Error with --output-directory: the file \"#{@cli_options.output_directory}\" is not a directory"
  end
end

#check_push_fileObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hiptest-publisher/cli_options_checker.rb', line 62

def check_push_file
  if cli_options.push
    globbed_files = Dir.glob(cli_options.push)
    if globbed_files.length == 0
      raise CliOptionError, "Error with --push: the file \"#{cli_options.push}\" does not exist or is not readable"
    elsif globbed_files.length == 1 && globbed_files == [cli_options.push]
      if !File.readable?(cli_options.push)
        raise CliOptionError, "Error with --push: the file \"#{cli_options.push}\" does not exist or is not readable"
      elsif !File.file?(cli_options.push)
        raise CliOptionError, "Error with --push: the file \"#{cli_options.push}\" is not a regular file"
      end
    end
  end
end

#check_secret_tokenObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hiptest-publisher/cli_options_checker.rb', line 42

def check_secret_token
  if absent?(cli_options.xml_file)
    if absent?(cli_options.token)
      raise CliOptionError, [
        "Missing argument --token: you must specify project secret token with --token=<project-token>",
        "",
        "The project secret token can be found on Hiptest in the settings section, under",
        "'Publication settings'. It is a sequence of numbers uniquely identifying your",
        "project.",
        "",
        "Note that settings section is available only to administrators of the project.",
      ].join("\n")
    end

    unless numeric?(cli_options.token)
      raise CliOptionError, "Invalid format --token=\"#{@cli_options.token}\": the project secret token must be numeric"
    end
  end
end

#check_test_run_idObject



116
117
118
119
120
# File 'lib/hiptest-publisher/cli_options_checker.rb', line 116

def check_test_run_id
  if present?(cli_options.test_run_id) && !numeric?(cli_options.test_run_id)
    raise CliOptionError, "Invalid format --test-run-id=\"#{@cli_options.test_run_id}\": the test run id must be numeric"
  end
end

#check_xml_fileObject



106
107
108
109
110
111
112
113
114
# File 'lib/hiptest-publisher/cli_options_checker.rb', line 106

def check_xml_file
  if cli_options.xml_file
    if !File.readable?(cli_options.xml_file)
      raise CliOptionError, "Error with --xml-file: the file \"#{cli_options.xml_file}\" does not exist or is not readable"
    elsif !File.file?(cli_options.xml_file)
      raise CliOptionError, "Error with --xml-file: the file \"#{cli_options.xml_file}\" is not a regular file"
    end
  end
end