From e3ffa2c1ff94e97290d7bb3358741ea6933ed820 Mon Sep 17 00:00:00 2001 From: Borislav Stanimirov Date: Wed, 12 Jan 2022 07:31:59 +0200 Subject: [PATCH] Separate class with utils for cache (no longer pure Hash) --- test/integration/lib.rb | 59 +++++++++++++++++++++++---------- test/integration/test_simple.rb | 19 +++++------ 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/test/integration/lib.rb b/test/integration/lib.rb index 94f130f..bb28ed4 100644 --- a/test/integration/lib.rb +++ b/test/integration/lib.rb @@ -65,7 +65,7 @@ class Project CommandResult.new *Open3.capture3("cmake -S #{@src_dir} -B #{@build_dir} #{extra_args}") end - class CMakeCacheValue + class CMakeCacheEntry def initialize(val, type, advanced, desc) @val = val @type = type @@ -78,24 +78,47 @@ class Project "(#{val.inspect} #{type}" + (advanced? ? ' ADVANCED)' : ')') end end + class CMakeCache + def initialize(entries) + @entries = entries + end + def self.from_dir(dir) + entries = {} + cur_desc = '' + file = File.join(dir, 'CMakeCache.txt') + return nil if !File.file?(file) + File.readlines(file).each { |line| + line.strip! + next if line.empty? + next if line.start_with? '#' # comment + if line.start_with? '//' + cur_desc += line[2..] + else + m = /(.+?)(-ADVANCED)?:([A-Z]+)=(.*)/.match(line) + raise "Error parsing '#{line}' in #{file}" if !m + entries[m[1]] = CMakeCacheEntry.new(m[4], m[3], !!m[2], cur_desc) + cur_desc = '' + end + } + CMakeCache.new entries + end + + def [](key) + e = @entries[key] + return nil if !e + e.val + end + + def get_package_data(package) + [ + self["CPM_PACKAGE_#{package}_VERSION"], + self["CPM_PACKAGE_#{package}_SOURCE_DIR"], + self["CPM_PACKAGE_#{package}_BINARY_DIR"], + ] + end + end def read_cache - vars = {} - cur_desc = '' - file = File.join(@build_dir, 'CMakeCache.txt') - File.readlines(file).each { |line| - line.strip! - next if line.empty? - next if line.start_with? '#' # comment - if line.start_with? '//' - cur_desc += line[2..] - else - m = /(.+?)(-ADVANCED)?:([A-Z]+)=(.*)/.match(line) - raise "Error parsing '#{line}' in #{file}" if !m - vars[m[1]] = CMakeCacheValue.new(m[4], m[3], !!m[2], cur_desc) - cur_desc = '' - end - } - vars + CMakeCache.from_dir @build_dir end end diff --git a/test/integration/test_simple.rb b/test/integration/test_simple.rb index 5485301..4053570 100644 --- a/test/integration/test_simple.rb +++ b/test/integration/test_simple.rb @@ -1,13 +1,7 @@ require './lib' class Simple < IntegrationTest - def get_adder_data(cache) - [ - cache['CPM_PACKAGE_testpack-adder_VERSION'].val, - cache['CPM_PACKAGE_testpack-adder_SOURCE_DIR'].val, - cache['CPM_PACKAGE_testpack-adder_BINARY_DIR'].val, - ] - end + P_ADDER = 'testpack-adder' def test_basics prj = make_project 'using-adder' @@ -19,9 +13,9 @@ class Simple < IntegrationTest cache = prj.read_cache - assert_equal 'testpack-adder', cache['CPM_PACKAGES'].val + assert_equal P_ADDER, cache['CPM_PACKAGES'] - ver, src, bin = get_adder_data cache + ver, src, bin = cache.get_package_data(P_ADDER) assert_equal ver, '0' assert File.directory? src @@ -35,8 +29,11 @@ class Simple < IntegrationTest prj.create_lists_with package: 'CPMAddPackage("gh:cpm-cmake/testpack-adder@1.0.0")' cfg_result = prj.configure - cache_new = prj.read_cache - ver, src, bin = get_adder_data cache_new + assert cfg_result.status.success? + + cache = prj.read_cache + + ver, src, bin = cache.get_package_data(P_ADDER) assert_equal '1.0.0', ver