Class: Pinspec::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/pinspec/cli.rb

Constant Summary collapse

ORDER =
%w[pin init analyze validate report plan capture version].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/pinspec/cli.rb', line 10

def self.exit_on_failure?
  true
end

.sort_commands!(list) ⇒ Object

Thor sorts the command list alphabetically after building it, which buries the one verb most people want between init and plan, and gives the two diagnostics equal billing with the product.



19
20
21
22
23
24
# File 'lib/pinspec/cli.rb', line 19

def self.sort_commands!(list)
  list.sort_by! do |usage, _|
    name = usage.to_s.split(/\s+/)[1].to_s
    [ORDER.index(name) || ORDER.size, name]
  end
end

Instance Method Details

#analyze(app_path = ".") ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/pinspec/cli.rb', line 98

def analyze(app_path = ".")
  guarded do
    Config.load(app_path)
    profile = Analyzer::AppProfileReader.read(app_path)

    print_app(profile)
    puts
    print_schema(profile.schema)
    puts
    print_factories(profile.factories)
    print_warnings(profile)
  end
end

#capture(*args) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/pinspec/cli.rb', line 125

def capture(*args)
  guarded do
    target = target_from(args)
    file, method = Analyzer::TargetParser.split_target(target)

    result = Runner::Capture.new(
      app_root:    options[:app],
      target:      file,
      method:      method,
      max_cases:   setting('cases', Inputs::Corpus::DEFAULT_MAX_CASES),
      boots:       setting('boots', 2),
      compare_sql: setting('compare-sql', false),
      sandbox_env: app_env,
      sample:      setting('sample', false),
      redact:      !options[:"no-redact"]
    ).run

    print_capture(result)

    raise NothingStableToPin, nothing_stable_message(result.stability) if result.stability.nothing_to_pin?
  end
end

#init(app_path = ".") ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pinspec/cli.rb', line 35

def init(app_path = ".")
  guarded do
    path = File.join(app_path, Config::FILENAME)

    if File.file?(path) && !options[:force]
      raise ConfigInvalid, "#{path} already exists. Pass --force to overwrite it."
    end

    runtime = Runner::Runtime.for(app_path)
    profile = Analyzer::AppProfileReader.read(app_path)

    # Deliberately NOT the detected environment. Writing the resolved PATH into a
    # committed file bakes in one machine's layout and goes stale the moment the
    # app changes Ruby - and detection re-runs on every invocation anyway. `env`
    # is for what pinspec cannot work out: database credentials, feature flags.
    File.write(path, Config.new(app_path, {
                                  "cases" => Inputs::Corpus::DEFAULT_MAX_CASES,
                                  "boots" => 2
                                }, path).to_yaml_document)

    row "wrote", path
    row "rails", profile.rails_version || "unknown"
    row "ruby", runtime.ruby_version
    row "runtime", runtime.detected? ? "#{runtime.manager}, detected on each run" : "this shell's Ruby"
    puts

    if runtime.note
      puts runtime.note
      puts
    end

    puts "Now: pinspec pin app/services/your_service.rb"
  end
end

#pin(*args) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/pinspec/cli.rb', line 164

def pin(*args)
  guarded do
    target = target_from(args)
    refuse_unbuilt_backend!
    warn_about_redaction!

    return pin_directory(target) if File.directory?(target)

    pin_one(target)
  end
end

#plan(target) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pinspec/cli.rb', line 74

def plan(target)
  guarded do
    file, method = Analyzer::TargetParser.split_target(target)
    target_profile = Analyzer::TargetParser.parse(file, method)

    print_profile(target_profile)
    puts

    app_profile = Analyzer::AppProfileReader.read(options[:app])
    setup_plan  = Setup::ContextBuilder.build(target: target_profile, profile: app_profile)
    corpus     = Inputs::Corpus.build(
      target:    target_profile,
      plan:      setup_plan,
      schema:    app_profile.schema,
      max_cases: setting('cases', Inputs::Corpus::DEFAULT_MAX_CASES)
    )

    print_plan(setup_plan)
    puts
    print_corpus(corpus)
  end
end

#reportObject



296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/pinspec/cli.rb', line 296

def report
  guarded do
    path = File.join(options[:app], Report::Summary::OUTPUT)

    unless File.file?(path)
      raise TargetNotFound,
            "no report at #{path}. Run `pinspec pin` first (or `pinspec validate`, " \
            "which adds the mutation scores); either writes one every time."
    end

    puts Analyzer::Source.read(path)
  end
end

#validate(*args) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/pinspec/cli.rb', line 260

def validate(*args)
  guarded do
    target = target_from(args)
    file, method = Analyzer::TargetParser.split_target(target)

    capture = Runner::Capture.new(
      app_root: options[:app], target: file, method: method,
      max_cases: setting('cases', Inputs::Corpus::DEFAULT_MAX_CASES), sandbox_env: app_env
    ).run

    raise NothingStableToPin, nothing_stable_message(capture.stability) if capture.stability.nothing_to_pin?

    profile = Analyzer::AppProfileReader.read(options[:app])

    report = Validate::PinScorer.new(
      app_root: options[:app], target: capture.target, plan: capture.plan,
      corpus: capture.corpus, stability: capture.stability,
      fk_map: profile.schema.fk_map,
      env: app_env, test_command: setting("test-command", nil)
    ).run

    print_scores(report)

    summary = Report::Summary.new(
      app_root: options[:app], profile: profile, target: capture.target,
      plan: capture.plan, corpus: capture.corpus, stability: capture.stability,
      scores: report
    )

    puts
    puts "  report         #{summary.write!}"
  end
end

#versionObject



27
28
29
30
# File 'lib/pinspec/cli.rb', line 27

def version
  puts "pinspec #{Pinspec::VERSION} " \
       "(probe v#{Pinspec::PROBE_VERSION}, serializer v#{Pinspec::SERIALIZER_VERSION})"
end