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.



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

def initialize(**args)
  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)
  end

  super(**args)
end

Class Method Details

.build_defaults(cli_defaults: true) ⇒ Object



56
57
58
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
# File 'lib/tldr/value/config.rb', line 56

def self.build_defaults cli_defaults: true
  common = {
    seed: rand(10_000),
    no_helper: false,
    verbose: 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



122
123
124
125
126
127
128
# File 'lib/tldr/value/config.rb', line 122

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/tldr/value/config.rb', line 100

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, :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: []) ⇒ Object



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

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

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

  argv.join(" ")
end

#to_single_path_args(path) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/tldr/value/config.rb', line 162

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

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

#undefault_parallel_if_seed_set(args) ⇒ Object



93
94
95
96
97
98
# File 'lib/tldr/value/config.rb', line 93

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.



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

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