From 57386e73e83888802f2d022e02002787c6b54d06 Mon Sep 17 00:00:00 2001 From: Borislav Stanimirov Date: Mon, 10 Jan 2022 07:47:17 +0200 Subject: [PATCH] Alternative approach. Using ruby's test/unit --- test/integration/runner-old.rb | 131 ++++++++++++++++ test/integration/runner.rb | 141 ++++-------------- .../{tests/simple.rb => simple-old.rb} | 0 test/integration/test_noop.rb | 10 ++ test/integration/tests/0-noop.rb | 4 - 5 files changed, 166 insertions(+), 120 deletions(-) create mode 100644 test/integration/runner-old.rb rename test/integration/{tests/simple.rb => simple-old.rb} (100%) create mode 100644 test/integration/test_noop.rb delete mode 100644 test/integration/tests/0-noop.rb diff --git a/test/integration/runner-old.rb b/test/integration/runner-old.rb new file mode 100644 index 0000000..db7cc97 --- /dev/null +++ b/test/integration/runner-old.rb @@ -0,0 +1,131 @@ +require 'fileutils' +require 'open3' + +CPMPath = File.expand_path('../../cmake/CPM.cmake', __dir__) +raise "Cannot file 'CPM.cmake' at '#{CPMPath}'" if !File.file?(CPMPath) + +CommonHeader = <<~CMAKE + cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + include("#{CPMPath}") + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +CMAKE + +TestDir = File.expand_path("./tmp/#{Time.now.strftime('%Y_%m_%d-%H_%M_%S')}") +raise "Test directory '#{TestDir}' already exists" if File.exist?(TestDir) + +puts "Running CPM.cmake integration tests" +puts "Temp directory: '#{TestDir}'" + +class CMakeListsBuilder + def initialize + @contents = '' + end + def literal(lit) + @contents += lit + "\n"; + self + end + def package(pack) + literal "CPMAddPackage(#{pack})" + end + def exe(exe, sources) + @contents += "add_executable(#{exe}\n" + @contents += sources.map { |src| + ' ' + if src['/'] + src + else + File.expand_path("#{src}", __dir__) + end + }.join("\n") + @contents += "\n)\n" + self + end + def link_libs(target, libs) + literal "target_link_libraries(#{target} #{libs})\n" + end + def to_s + @contents + end +end + +class ExecuteResult + def initialize(out, err, status) + @out = out + @err = err + @status = status + end + attr :out, :err, :status +end + +class Project + def initialize(name) + @name = name + @dir = File.join(TestDir, name) + + FileUtils.mkdir_p(File.join(TestDir, name)) + end + + def build_cmake_lists(opts = {}, &block) + builder = CMakeListsBuilder.new + if !opts[:no_default_header] + builder.literal(CommonHeader) + builder.literal("project(#{@name})") + end + text = builder.instance_eval &block + + File.write(File.join(@dir, 'CMakeLists.txt'), text) + end + + def configure(args = '') + ExecuteResult.new *Open3.capture3("cmake . #{args}", chdir: @dir) + end +end + +@cur_file = '' +@tests = {} +def add_test(name, func) + raise "#{@cur_file}: Test #{name} is already defined from another file" if @tests[name] + @tests[name] = func +end + +# check funcs +class CheckFail < StandardError + def initialize(msg) + super + end +end + +def check(b) + raise CheckFail.new "expected 'true'" if !b +end + +Dir['tests/*.rb'].sort.each do |file| + @cur_file = file + load './' + file +end + +# sort alphabetically +sorted_tests = @tests.to_a.sort {|a, b| a[0] <=> b[0] } + +num_succeeded = 0 +num_failed = 0 + +sorted_tests.each do |name, func| + puts "Running '#{name}'" + proj = Project.new(name) + begin + func.(proj) + num_succeeded += 1 + puts ' success' + rescue CheckFail => error + num_failed += 1 + STDERR.puts " #{name}: check failed '#{error.message}'" + STDERR.puts " backtrace:\n #{error.backtrace.join("\n ")}" + STDERR.puts + end +end + +puts "Ran #{num_succeeded + num_failed} tests" +puts "Succeeded: #{num_succeeded}" +puts "Failed: #{num_failed}" + +exit(num_failed) diff --git a/test/integration/runner.rb b/test/integration/runner.rb index d66b362..2a0dfd6 100644 --- a/test/integration/runner.rb +++ b/test/integration/runner.rb @@ -1,128 +1,37 @@ require 'fileutils' require 'open3' +require 'tmpdir' +require 'test/unit' -CPMPath = File.expand_path('../../cmake/CPM.cmake') +TestTmpDir = File.join(Dir.tmpdir, "cpm-itest-#{Time.now.strftime('%Y_%m_%d-%H_%M_%S')}") +raise "Test directory '#{TestTmpDir}' already exists" if File.exist?(TestTmpDir) + +puts "Running CPM.cmake integration tests" +puts "Temp directory: '#{TestTmpDir}'" + +CPMPath = File.expand_path('../../cmake/CPM.cmake', __dir__) raise "Cannot file 'CPM.cmake' at '#{CPMPath}'" if !File.file?(CPMPath) -CommonHeader = <<~CMAKE - cmake_minimum_required(VERSION 3.14 FATAL_ERROR) - include("#{CPMPath}") - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") -CMAKE +# Environment variables which are read by cpm +CPM_ENV = %w( + CPM_USE_LOCAL_PACKAGES + CPM_LOCAL_PACKAGES_ONLY + CPM_DOWNLOAD_ALL + CPM_DONT_UPDATE_MODULE_PATH + CPM_DONT_CREATE_PACKAGE_LOCK + CPM_INCLUDE_ALL_IN_PACKAGE_LOCK + CPM_USE_NAMED_CACHE_DIRECTORIES + CPM_SOURCE_CACHE +) -TestDir = File.expand_path("./tmp/#{Time.now.strftime('%Y_%m_%d-%H_%M_%S')}") -raise "Test directory '#{TestDir}' already exists" if File.exist?(TestDir) - -class CMakeListsBuilder - def initialize - @contents = '' - end - def literal(lit) - @contents += lit + "\n"; - self - end - def package(pack) - literal "CPMAddPackage(#{pack})" - end - def exe(exe, sources) - @contents += "add_executable(#{exe}\n" - @contents += sources.map { |src| - ' ' + if src['/'] - src - else - File.expand_path("./#{src}") - end - }.join("\n") - @contents += "\n)\n" - self - end - def link_libs(target, libs) - literal "target_link_libraries(#{target} #{libs})\n" - end - def to_s - @contents - end -end - -class ExecuteResult - def initialize(out, err, status) - @out = out - @err = err - @status = status - end - attr :out, :err, :status -end +# Clear existing cpm-related env vars +CPM_ENV.each { ENV[_1] = nil } class Project - def initialize(name) - @name = name - @dir = File.join(TestDir, name) - - FileUtils.mkdir_p(File.join(TestDir, name)) - end - - def build_cmake_lists(opts = {}, &block) - builder = CMakeListsBuilder.new - if !opts[:no_default_header] - builder.literal(CommonHeader) - builder.literal("project(#{@name})") - end - text = builder.instance_eval &block - - File.write(File.join(@dir, 'CMakeLists.txt'), text) - end - - def configure(args = '') - ExecuteResult.new *Open3.capture3("cmake . #{args}", chdir: @dir) + def initialize(dir) + @dir = File.join(TestTmpDir, dir) end end -@cur_file = '' -@tests = {} -def add_test(name, func) - raise "#{@cur_file}: Test #{name} is already defined from another file" if @tests[name] - @tests[name] = func -end +# exit Test::Unit::AutoRunner::run(true, __dir__) -# check funcs -class CheckFail < StandardError - def initialize(msg) - super - end -end - -def check(b) - raise CheckFail.new "expected 'true'" if !b -end - -Dir['tests/*.rb'].sort.each do |file| - @cur_file = file - load './' + file -end - -# sort alphabetically -sorted_tests = @tests.to_a.sort {|a, b| a[0] <=> b[0] } - -num_succeeded = 0 -num_failed = 0 - -sorted_tests.each do |name, func| - puts "Running '#{name}'" - proj = Project.new(name) - begin - func.(proj) - num_succeeded += 1 - puts ' success' - rescue CheckFail => error - num_failed += 1 - STDERR.puts " #{name}: check failed '#{error.message}'" - STDERR.puts " backtrace:\n #{error.backtrace.join("\n ")}" - STDERR.puts - end -end - -puts "Ran #{num_succeeded + num_failed} tests" -puts "Succeeded: #{num_succeeded}" -puts "Failed: #{num_failed}" - -exit(num_failed) diff --git a/test/integration/tests/simple.rb b/test/integration/simple-old.rb similarity index 100% rename from test/integration/tests/simple.rb rename to test/integration/simple-old.rb diff --git a/test/integration/test_noop.rb b/test/integration/test_noop.rb new file mode 100644 index 0000000..02c7288 --- /dev/null +++ b/test/integration/test_noop.rb @@ -0,0 +1,10 @@ +# this test does nothing +# it's, in a way, a test of the integration testing framework + +class Noop < Test::Unit::TestCase + def test_tt + puts 'run' + assert true + assert_equal 1, 1 + end +end diff --git a/test/integration/tests/0-noop.rb b/test/integration/tests/0-noop.rb deleted file mode 100644 index 5eb6dc1..0000000 --- a/test/integration/tests/0-noop.rb +++ /dev/null @@ -1,4 +0,0 @@ -# this test does nothing -# it's, in a way, a test of the integration testing framework - -puts 'executing noop test'