Class: TLDR::Config

Inherits:
Struct
  • Object
show all
Defined in:
lib/tldr/value/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Config

Returns a new instance of Config.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tldr/value/config.rb', line 39

def initialize(**args)
  original_base_path = Dir.pwd
  unless args[:config_intended_for_merge_only]
    change_working_directory_because_i_am_bad_and_i_should_feel_bad!(args[:base_path])
    args = merge_dotfile_args(args) unless args[:no_dotfile]
  end
  args = undefault_parallel_if_seed_set(args)
  unless args[:config_intended_for_merge_only]
    args = merge_defaults(args)
    revert_working_directory_change_because_itll_ruin_everything!(original_base_path)
  end

  super(**args)
end

Class Method Details

.build_defaults(cli_defaults: true) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/tldr/value/config.rb', line 59

def self.build_defaults cli_defaults: true
  common = {
    seed: rand(10_000),
    no_helper: false,
    verbose: false,
    print_interrupted_test_backtraces: false,
    reporter: Reporters::Default,
    parallel: true,
    names: [],
    fail_fast: false,
    no_emoji: false,
    no_prepend: false,
    exclude_paths: [],
    exclude_names: [],
    base_path: nil,
    warnings: true,
    watch: false,
    yes_i_know: false,
    i_am_being_watched: false
  }

  if cli_defaults
    common.merge(
      paths: Dir["test/**/*_test.rb", "test/**/test_*.rb"],
      helper_paths: ["test/helper.rb"],
      load_paths: ["lib", "test"],
      prepend_paths: [MOST_RECENTLY_MODIFIED_TAG]
    )
  else
    common.merge(
      paths: [],
      helper_paths: [],
      load_paths: [],
      prepend_paths: []
    )
  end
end

Instance Method Details

#merge(other) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/tldr/value/config.rb', line 126

def merge other
  this_config = to_h
  kwargs = this_config.merge(
    other.to_h.compact.except(:config_intended_for_merge_only)
  )
  Config.new(**kwargs)
end

#merge_defaults(user_args) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/tldr/value/config.rb', line 104

def merge_defaults user_args
  merged_args = user_args.dup
  defaults = Config.build_defaults(cli_defaults: merged_args[:cli_defaults])

  # Arrays
  [:paths, :helper_paths, :load_paths, :names, :prepend_paths, :exclude_paths, :exclude_names].each do |key|
    merged_args[key] = defaults[key] if merged_args[key].nil? || merged_args[key].empty?
  end

  # Booleans
  [:no_helper, :verbose, :print_interrupted_test_backtraces, :fail_fast, :no_emoji, :no_prepend, :warnings, :yes_i_know, :i_am_being_watched].each do |key|
    merged_args[key] = defaults[key] if merged_args[key].nil?
  end

  # Values
  [:seed, :reporter].each do |key|
    merged_args[key] ||= defaults[key]
  end

  merged_args
end

#to_full_args(exclude: [], ensure_args: [], exclude_dotfile_matches: false) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/tldr/value/config.rb', line 149

def to_full_args exclude: [], ensure_args: [], exclude_dotfile_matches: false
  argv = to_cli_argv(
    CONFLAGS.keys - exclude - [
      (:seed unless seed_set_intentionally),
      :watch,
      :i_am_being_watched
    ],
    exclude_dotfile_matches:
  )

  ensure_args.each do |arg|
    argv << arg unless argv.include?(arg)
  end

  argv.join(" ")
end

#to_single_path_args(path, exclude_dotfile_matches: false) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/tldr/value/config.rb', line 166

def to_single_path_args path, exclude_dotfile_matches: false
  argv = to_cli_argv(CONFLAGS.keys - [
    :seed, :parallel, :names, :fail_fast, :paths, :prepend_paths,
    :no_prepend, :exclude_paths, :watch, :i_am_being_watched
  ], exclude_dotfile_matches:)

  (argv + [stringify(:paths, path)]).join(" ")
end

#undefault_parallel_if_seed_set(args) ⇒ Object



97
98
99
100
101
102
# File 'lib/tldr/value/config.rb', line 97

def undefault_parallel_if_seed_set args
  args.merge(
    seed_set_intentionally: !args[:seed].nil?,
    parallel: (args[:parallel].nil? ? args[:seed].nil? : args[:parallel])
  )
end

#update_after_gathering_tests!(tests) ⇒ Object

We needed this hook (to be called by the planner), because we can’t know the default prepend location until we have all the resolved test paths, so we have to mutate it after the fact.



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/tldr/value/config.rb', line 137

def update_after_gathering_tests! tests
  return unless prepend_paths.include?(MOST_RECENTLY_MODIFIED_TAG)

  self.prepend_paths = prepend_paths.map { |path|
    if path == MOST_RECENTLY_MODIFIED_TAG
      most_recently_modified_test_file(tests)
    else
      path
    end
  }.compact
end