lib/rspec/terraspace/project.rb in rspec-terraspace-0.0.0 vs lib/rspec/terraspace/project.rb in rspec-terraspace-0.1.0

- old
+ new

@@ -3,47 +3,110 @@ module RSpec::Terraspace class Project def initialize(options={}) @options = options - @name = options[:name] || "demo" + @name = options[:name] + @config = options[:config] @modules = options[:modules] @stacks = options[:stacks] + @tfvars = options[:tfvars] + @folders = options[:folders] @remove_test_folder = options[:remove_test_folder].nil? ? true : options[:remove_test_folder] end def create clean build_project + build_config build_modules build_stacks + build_tfvars + build_folders puts "Test harness built: #{build_dir}" build_dir end + # folders at any-level, including top-level can be copied with the folders option + 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 + 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) - ::Terraspace::CLI::New::Project.start([project_name]) + ::Terraspace::CLI::New::Project.start([project_name, "--no-config"]) end + end - # TODO: terraspace new project --no-config option instead - FileUtils.rm_f("#{build_dir}/config/backend.tf") - FileUtils.rm_f("#{build_dir}/config/provider.tf") + def build_config + return unless @config + + config_folder = "#{build_dir}/config" + FileUtils.rm_rf(config_folder) # wipe current config folder + FileUtils.mkdir_p(File.dirname(config_folder)) + src = @config + FileUtils.cp_r(src, config_folder) end def build_modules - build_app_subfolder(@modules, "modules") + return unless @modules + build_type_folder("modules", @modules) end def build_stacks - build_app_subfolder(@stacks, "stacks") + return unless @stacks + build_type_folder("stacks", @stacks) end + # If a file has been supplied, then it gets copied over. + # + # # File + # terraspace.build_test_harness( + # tfvars: {demo: "spec/fixtures/tfvars/demo.tfvars"}, + # end + # + # # Results in: + # app/stacks/#{stack}/tfvars/test.tfvars + # + # If a directory has been supplied, then the folder fully gets copied over. + # + # # Directory + # terraspace.build_test_harness( + # tfvars: {demo: "spec/fixtures/tfvars/demo"}, + # end + # + # # Results in (whatever is in the folder): + # app/stacks/#{stack}/tfvars/base.tfvars + # app/stacks/#{stack}/tfvars/test.tfvars + # + def build_tfvars + return unless @tfvars + @tfvars.each do |stack, src| + tfvars_folder = "#{build_dir}/app/stacks/#{stack}/tfvars" + FileUtils.rm_rf(tfvars_folder) # wipe current tfvars folder. dont use any of the live values + + if File.directory?(src) + FileUtils.mkdir_p(File.dirname(tfvars_folder)) + FileUtils.cp_r(src, tfvars_folder) + else # if only a single file, then generate a test.tfvars since this runs under TS_ENV=test + dest = "#{tfvars_folder}/test.tfvars" + FileUtils.mkdir_p(File.dirname(dest)) + FileUtils.cp(src, dest) + end + end + end + # Inputs: # # list: options[:modules] or options[:stacks] # type_dir: modules or stacks # @@ -56,11 +119,11 @@ # This allows for finer-control to specify what modules and stacks to build # # If provide a String, it should be a path to folder containing all modules or stacks. # This provides less fine-grain control but is easier to use and shorter. # - def build_app_subfolder(list, type_dir) + 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) @@ -87,13 +150,17 @@ FileUtils.mkdir_p(File.dirname(dest)) FileUtils.cp_r(src, dest) end def build_dir - "#{build_root}/#{@name}" + "#{tmp_root}/#{@name}" end - def build_root - ENV['TS_RSPEC_BUILD_ROOT'] || "/tmp/terraspace/test-harnesses" + def tmp_root + self.class.tmp_root + end + + def self.tmp_root + "#{Terraspace.tmp_root}/test-harnesses" end end end