Class: TLDR::Config

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

Constant Summary collapse

DEFAULT_YAML_PATH =
".tldr.yml"
DEFAULT_TIMEOUT =
1.8

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Config

Returns a new instance of Config.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tldr/value/config.rb', line 43

def initialize(**args)
  @argv_reconstructor = ArgvReconstructor.new

  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[:config_path].nil?
  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
end

Class Method Details

.build_defaults(cli_defaults: true) ⇒ Object



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
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/tldr/value/config.rb', line 65

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

  if cli_defaults
    common.merge(
      helper_paths: ["test/helper.rb"],
      prepend_paths: [MOST_RECENTLY_MODIFIED_TAG],
      load_paths: ["lib", "test"],
      config_path: nil,
      paths: Dir["test/**/*_test.rb", "test/**/test_*.rb"]
    )
  else
    common.merge(
      helper_paths: [],
      prepend_paths: [],
      load_paths: [],
      config_path: Config::DEFAULT_YAML_PATH, # ArgvParser#parse will set this default and if it sets nil that is intentionally blank b/c --no-config
      paths: []
    )
  end
end

Instance Method Details

#dotfile_args(config_path) ⇒ Object



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

def dotfile_args config_path
  return {} unless File.exist?(config_path)

  @dotfile_args ||= YamlParser.new.parse(config_path)
end

#merge(other) ⇒ Object



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

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/tldr/value/config.rb', line 115

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

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

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

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

  merged_args
end

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



160
161
162
# File 'lib/tldr/value/config.rb', line 160

def to_full_args exclude: [], ensure_args: [], exclude_dotfile_matches: false
  @argv_reconstructor.reconstruct(self, exclude:, ensure_args:, exclude_dotfile_matches:)
end

#to_single_path_args(path, exclude_dotfile_matches: false) ⇒ Object



164
165
166
# File 'lib/tldr/value/config.rb', line 164

def to_single_path_args path, exclude_dotfile_matches: false
  @argv_reconstructor.reconstruct_single_path_args(self, path, exclude_dotfile_matches:)
end

#undefault_parallel_if_seed_set(args) ⇒ Object



108
109
110
111
112
113
# File 'lib/tldr/value/config.rb', line 108

def undefault_parallel_if_seed_set args
  args.merge(
    parallel: (args[:parallel].nil? ? args[:seed].nil? : args[:parallel]),
    seed_set_intentionally: !args[:seed].nil?
  )
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.



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/tldr/value/config.rb', line 148

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