Class: DeployDoc::TestPlan

Inherits:
Object
  • Object
show all
Defined in:
lib/deploy_doc/test_plan.rb,
lib/deploy_doc/test_plan/annotator_parser.rb

Defined Under Namespace

Classes: AnnotationParser, Step

Constant Summary collapse

PHASES =
["pre-install", "create-infrastructure", "run-tests", "destroy-infrastructure"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata, required_env_vars, steps_in_phases) ⇒ TestPlan

Returns a new instance of TestPlan.



52
53
54
55
56
# File 'lib/deploy_doc/test_plan.rb', line 52

def initialize(, required_env_vars, steps_in_phases)
  @metadata = 
  @required_env_vars = required_env_vars
  @steps_in_phases = steps_in_phases
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



48
49
50
# File 'lib/deploy_doc/test_plan.rb', line 48

def 
  @metadata
end

#required_env_varsObject (readonly)

Returns the value of attribute required_env_vars.



49
50
51
# File 'lib/deploy_doc/test_plan.rb', line 49

def required_env_vars
  @required_env_vars
end

#steps_in_phasesObject (readonly)

Returns the value of attribute steps_in_phases.



50
51
52
# File 'lib/deploy_doc/test_plan.rb', line 50

def steps_in_phases
  @steps_in_phases
end

Class Method Details

.from_file(file_name) ⇒ Object



13
14
15
16
# File 'lib/deploy_doc/test_plan.rb', line 13

def self.from_file(file_name)
  content = File.read(file_name)
  self.from_str(content, file_name)
end

.from_str(content, file_name = "<unknown file>") ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/deploy_doc/test_plan.rb', line 18

def self.from_str(content, file_name="<unknown file>")
   = self.(content, file_name)

  if ["deployDoc"] != true
    raise DeployDoc::Error.new("Markdown file #{file_name} does not have a 'deployDoc:true' metadatum")
  end

  annotations = AnnotationParser.parse(content, file_name)
  required_env_vars = (annotations.select { |a| a.kind == "require-env" }).map { |a| a.params }.flatten
  phases = self.phases_from_annotations(annotations)
  TestPlan.new(, required_env_vars, phases)
end

.parse_metadata(content, file_name) ⇒ Object



31
32
33
34
35
36
# File 'lib/deploy_doc/test_plan.rb', line 31

def self.(content, file_name)
   = content.split("---",3)[1]
  YAML.load()
rescue
  raise Error.new("Could not parse metadata in #{file_name}")
end

.phases_from_annotations(annotations) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/deploy_doc/test_plan.rb', line 38

def self.phases_from_annotations(annotations)
  phases = {}

  PHASES.each do |phase|
    phases[phase] = (annotations.select { |a| a.kind == phase }).map { |a| Step.new(a.source_name, a.line_span, a.content) }
  end

  phases
end

Instance Method Details

#missing_env_varsObject



79
80
81
# File 'lib/deploy_doc/test_plan.rb', line 79

def missing_env_vars
  @required_env_vars.select { |e| ENV[e].nil? }
end

#to_jsonObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/deploy_doc/test_plan.rb', line 83

def to_json
  json = {}

  PHASES.each do |phase_name|
    json[phase_name] = []
    @steps_in_phases[phase_name].each do |step|
      json[phase_name].push({
        line_span: step.line_span.inspect, 
        shell: step.shell.strip
      })
    end
  end

  JSON.pretty_generate(json)
end

#to_sObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/deploy_doc/test_plan.rb', line 58

def to_s
 parts = []
 parts << "Deployment test plan:"
 parts << ""
 parts << "Required environment parameters"

 @required_env_vars.each do |e|
   parts << "  - #{e}"
 end

 PHASES.each do |phase|
   parts << "Steps in phase #{phase}:"
   @steps_in_phases[phase].each do |step|
     parts << "- #{step.source_name}:#{step.line_span.inspect}"
     parts << step.shell
   end
 end

 parts.join("\n")
end