Class: Pinspec::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/pinspec/cli.rb', line 7

def self.exit_on_failure?
  true
end

Instance Method Details

#analyze(app_path = ".") ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pinspec/cli.rb', line 46

def analyze(app_path = ".")
  guarded do
    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(target) ⇒ Object



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

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

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

    print_capture(result)

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

#pin(target) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/pinspec/cli.rb', line 110

def pin(target)
  guarded do
    refuse_unbuilt_backend!
    warn_about_redaction!
    file, method = Analyzer::TargetParser.split_target(target)

    capture = Runner::Capture.new(
      app_root: options[:app], target: file, method: method,
      max_cases: options[:cases], boots: options[:boots], sandbox_env: app_env,
      sample: options[:sample], redact: !options[:"no-redact"]
    ).run

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

    written = Emit::SpecWriter.new(
      app_root: options[:app], target: capture.target, plan: capture.plan,
      corpus: capture.corpus, stability: capture.stability,
      fk_map: Analyzer::AppProfileReader.read(options[:app]).schema.fk_map,
      force: options[:force]
    ).write!

    puts
    print_written(written)

    return if options[:"skip-verify"]

    outcomes = Verify::Verifier.new(
      app_root: options[:app], spec_path: written.spec_path,
      level: options[:"verify-level"].to_sym, env: app_env,
      captured_tz: capture.plan.env_fingerprint[:tz]
    ).verify

    puts
    print_verification(outcomes)

    report_path = Report::Summary.new(
      app_root: options[:app], profile: Analyzer::AppProfileReader.read(options[:app]),
      target: capture.target, plan: capture.plan, corpus: capture.corpus,
      stability: capture.stability, written: written, outcomes: outcomes
    ).write!

    puts
    row "report", report_path

    raise VerifyFailed, verify_failed_message(outcomes) unless outcomes.all?(&:green?)
  end
end

#plan(target) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pinspec/cli.rb', line 22

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: options[:cases]
    )

    print_plan(setup_plan)
    puts
    print_corpus(corpus)
  end
end

#reportObject



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/pinspec/cli.rb', line 201

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(target) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/pinspec/cli.rb', line 166

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

    capture = Runner::Capture.new(
      app_root: options[:app], target: file, method: method,
      max_cases: options[: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: options[:"test-command"]
    ).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



12
13
14
15
# File 'lib/pinspec/cli.rb', line 12

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