Class: RSpec::Lono::Harness::Project

Inherits:
Object
  • Object
show all
Includes:
Concerns
Defined in:
lib/rspec/lono/harness/project.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns

#detection

Methods included from Concerns::Logging

#logger

Constructor Details

#initialize(options = {}) ⇒ Project

Returns a new instance of Project.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rspec/lono/harness/project.rb', line 8

def initialize(options={})
  @config     = options[:config] || "spec/fixtures/config"
  @blueprint  = options[:blueprint] || detection.blueprint # String. IE: demo
  @params     = options[:params] || "spec/fixtures/params"
  @vars       = options[:vars] || "spec/fixtures/vars"
  @folders    = options[:folders]
  @plugin     = options[:plugin]

  @root       = options[:root] || detection.root # IE: /home/user/lono-project/app/blueprints/demo
  @remove_test_folder = options[:remove_test_folder].nil? ? true : options[:remove_test_folder]
end

Class Method Details

.tmp_rootObject



170
171
172
# File 'lib/rspec/lono/harness/project.rb', line 170

def self.tmp_root
  "#{Lono.tmp_root}/test-harnesses"
end

Instance Method Details

#build_blueprintObject



63
64
65
66
# File 'lib/rspec/lono/harness/project.rb', line 63

def build_blueprint
  return unless @blueprint
  build_type_folder("blueprints", @blueprint => @root)
end

#build_configObject



54
55
56
57
58
59
60
61
# File 'lib/rspec/lono/harness/project.rb', line 54

def build_config
  return unless File.exist?(@config)
  config_folder = "#{build_dir}/config"
  FileUtils.mkdir_p(File.dirname(config_folder))
  Dir.glob("#{@config}/*").each do |src|
    FileUtils.cp_r(src, config_folder)
  end
end

#build_dirObject



162
163
164
# File 'lib/rspec/lono/harness/project.rb', line 162

def build_dir
  "#{tmp_root}/#{@blueprint}"
end

#build_foldersObject

folders at any-level, including top-level can be copied with the folders option



34
35
36
37
38
39
40
41
42
# File 'lib/rspec/lono/harness/project.rb', line 34

def build_folders
  return unless @folders

  @folders.each do |folder|
    dest = "#{build_dir}/#{folder}"
    FileUtils.mkdir_p(File.dirname(dest))
    FileUtils.cp_r(folder, dest)
  end
end

#build_inputs(type) ⇒ Object

If a file has been supplied, then it gets copied over.

# File
lono.build_test_harness(
  params: "spec/fixtures/params/demo.txt",
end

# Results in:
app/blueprints/#{@blueprint}/params/test.txt

If a directory has been supplied, then the folder fully gets copied over.

# Directory
lono.build_test_harness(
  params: {demo: "spec/fixtures/params/demo"},
end

# Results in (whatever is in the folder):
app/blueprints/#{@blueprint}/params/base.txt
app/blueprints/#{@blueprint}/params/test.txt


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rspec/lono/harness/project.rb', line 97

def build_inputs(type)
  instance_var = instance_variable_get("@#{type}") # IE: @params or @vars
  inputs = [instance_var].compact.flatten
  ext = type == :params ? 'txt' : 'rb'
  inputs.each do |src|
    folder = "#{build_dir}/config/blueprints/#{@blueprint}/#{type}"
    FileUtils.rm_rf(folder) # wipe current params folder

    if File.directory?(src)
      FileUtils.mkdir_p(File.dirname(folder))
      FileUtils.cp_r(src, folder)
    elsif File.exist?(src) # if only a single file, then generate a test.txt since this runs under LONO_ENV=test
      dest = "#{folder}/#{Lono.env}.#{ext}"
      FileUtils.mkdir_p(File.dirname(dest))
      FileUtils.cp(src, dest)
    end
  end
end

#build_paramsObject



68
69
70
# File 'lib/rspec/lono/harness/project.rb', line 68

def build_params
  build_inputs(:params)
end

#build_projectObject



44
45
46
47
48
49
50
51
52
# File 'lib/rspec/lono/harness/project.rb', line 44

def build_project
  parent_dir = File.dirname(build_dir)
  FileUtils.mkdir_p(parent_dir)
  Dir.chdir(parent_dir) do
    project_name = File.basename(build_dir)
    args = [project_name, "--quiet"]
    Lono::CLI::New::Project.start(args)
  end
end

#build_type_folder(type_dir, list) ⇒ Object

Inputs:

list:     options[:blueprints] or options[:stacks]
type_dir: blueprints or stacks

The list argument can support a Hash or String value.

If provided a Hahs, it should be structured like so:

{vm: "app/blueprints/vm", network: "app/blueprints/network"}

This allows for finer-control to specify what blueprints and stacks to build

If provide a String, it should be a path to folder containing all blueprints or stacks. This provides less fine-grain control but is easier to use and shorter.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rspec/lono/harness/project.rb', line 132

def build_type_folder(type_dir, list)
  case list
  when Hash
    list.each do |name, src|
      dest = "#{build_dir}/app/#{type_dir}/#{name}"
      copy(src, dest)
      remove_test_folder(dest) if @remove_test_folder
    end
  when String
    dest = "#{build_dir}/app/#{type_dir}"
    FileUtils.rm_rf(dest)
    FileUtils.cp_r(list, dest)
  else
    raise "blueprints option must be a Hash or String"
  end
end

#build_varsObject



72
73
74
# File 'lib/rspec/lono/harness/project.rb', line 72

def build_vars
  build_inputs(:vars)
end

#cleanObject



153
154
155
# File 'lib/rspec/lono/harness/project.rb', line 153

def clean
  FileUtils.rm_rf(build_dir)
end

#copy(src, dest) ⇒ Object



157
158
159
160
# File 'lib/rspec/lono/harness/project.rb', line 157

def copy(src, dest)
  FileUtils.mkdir_p(File.dirname(dest))
  FileUtils.cp_r(src, dest)
end

#createObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rspec/lono/harness/project.rb', line 20

def create
  puts "Building test harness at: #{build_dir}"
  clean
  build_project
  build_config
  build_blueprint
  build_params
  build_vars
  build_folders
  puts "Test harness built."
  build_dir
end

#remove_test_folder(dest) ⇒ Object



149
150
151
# File 'lib/rspec/lono/harness/project.rb', line 149

def remove_test_folder(dest)
  FileUtils.rm_rf("#{dest}/test")
end

#tmp_rootObject



166
167
168
# File 'lib/rspec/lono/harness/project.rb', line 166

def tmp_root
  self.class.tmp_root
end