From 6403e4f04fc8e29fb2603c70cf10e91b3b92c44e Mon Sep 17 00:00:00 2001 From: Ed Reel Date: Sun, 14 Mar 2021 21:32:31 -0500 Subject: [PATCH] Update tests (#5408) * Update tests * Improve speed of cycle_test and load_test scripts --- tests/README.md | 16 +++++++------ tests/{no_cycles.rb => cycle_test} | 38 ++++++++++++++++++++---------- tests/load_test | 15 ++++++++++++ tests/test_all | 21 ----------------- 4 files changed, 49 insertions(+), 41 deletions(-) rename tests/{no_cycles.rb => cycle_test} (58%) mode change 100644 => 100755 create mode 100755 tests/load_test delete mode 100755 tests/test_all diff --git a/tests/README.md b/tests/README.md index ec8cd21e8..4f5e1ef9c 100644 --- a/tests/README.md +++ b/tests/README.md @@ -2,22 +2,23 @@ ## Why to run tests -Running these tests will make sure that crew is working correctly, or explain the problem if it's not. +Running tests will make sure that crew is working correctly or explain the problem if it's not. ## When to run tests -The tests can be used at any time to help with developing, but make sure to run them before submitting a Pull Request. +The tests can be used at any time to help with development but make sure to run them before submitting a Pull Request. ## Preparing new tests -To prepare a new test, create a `.rb` file. It may use crew constants (created in `lib/const.rb`) and import packages (be sure to use `require_relative`). +To prepare a new test, create a ruby script. It may use crew constants (created in `lib/const.rb`) and import packages (be sure to use `require_relative`). -It should finish normally if the test passed, or finish with `exit 1` otherwise, preferably explaining why the test failed. -All `.rb` files in this directory will automatically be run by the `all_tests` script. +It should finish normally if the test passed, otherwise abort with `exit 1` and preferably explain why the test failed. -Here is a simple example of a test: +A simple example of a test script called `my_test` is below: ```ruby +#!/usr/bin/env ruby + # Makes sure buildessential package depends on gcc require_relative("../packages/buildessential") @@ -29,9 +30,10 @@ else exit 1 end ``` +Make sure the script is executable with `chmod +x my_test`. Note: This will only work in directories with execute permission. ## How to run tests -Execute `ruby test_all` from this directory (`tests/`). If all tests pass, it will print `All tests successful.`, otherwise it will show what went wrong. +Execute `ruby my_test` from this directory (`tests/`). If all tests pass, it should display a message similar to `All tests successful.`, otherwise it should indicate what went wrong. Running tests manually may not work! diff --git a/tests/no_cycles.rb b/tests/cycle_test old mode 100644 new mode 100755 similarity index 58% rename from tests/no_cycles.rb rename to tests/cycle_test index c842c41dc..f243d0add --- a/tests/no_cycles.rb +++ b/tests/cycle_test @@ -1,19 +1,24 @@ +#!/usr/bin/env ruby + # This test checks whether the packages create a dependency cycle. require 'find' +require_relative '../lib/const' require_relative '../lib/color' @all_pkgs = {} +$LOAD_PATH.unshift '../lib' + +puts "Running dependency cycle tests...\n".yellow + # Loads all packages -Find.find("#{CUR_DIR}/../packages") do |filename| - if File.extname(filename) == '.rb' - name = File.basename(filename, '.rb') - require_relative("../packages/#{name}") - pkg = Object.const_get(name.capitalize) - pkg.name = name - @all_pkgs[name] = pkg - end +Dir.glob('../packages/*.rb').each do |filename| + name = File.basename(filename, '.rb') + require_relative("../packages/#{name}") + pkg = Object.const_get(name.capitalize) + pkg.name = name + @all_pkgs[name] = pkg end # Looking for cycles. @path will keep the current dependency path. @@ -23,15 +28,17 @@ end @failed = 0 @state = {} @path = [] +@uniq_path = [] def dfs(pkg) @path.push(pkg.name) if @state[pkg] == :on_path - puts "\nFound dependency cycle!".lightred while @path.first != @path.last @path.shift end - puts @path.to_s - @failed += 1 + if not @uniq_path.include? @path.to_s and @path.to_s.include? ',' + @uniq_path.push(@path.to_s) + @failed += 1 + end elsif @state[pkg] == nil @state[pkg] = :on_path if pkg.dependencies @@ -51,11 +58,16 @@ end dfs(pkg) end +# Display dependency cycles +@uniq_path.sort.each do |path| + puts path.lightred +end + @cycles = "cycles" @cycles = "cycle" if @failed == 1 if @failed > 0 -abort "\n#{@failed} dependency #{@cycles} found.".lightred + abort "\n#{@failed} dependency #{@cycles} found.".lightred else -puts "\nNo dependency cycles found.".lightgreen + puts "\nNo dependency cycles found.".lightgreen end diff --git a/tests/load_test b/tests/load_test new file mode 100755 index 000000000..4d421a1c3 --- /dev/null +++ b/tests/load_test @@ -0,0 +1,15 @@ +#!/usr/bin/env ruby + +require 'find' +require_relative '../lib/const' +require_relative '../lib/color' + +# Add >LOCAL< lib to LOAD_PATH +$LOAD_PATH.unshift '../lib' + +Dir.glob('../packages/*.rb').each do |filename| + puts "Loading #{File.basename(filename, '.rb')}..." + load filename +end + +puts "\nAll load tests successful.".lightgreen diff --git a/tests/test_all b/tests/test_all deleted file mode 100755 index c2f6dd1c5..000000000 --- a/tests/test_all +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env ruby - -require 'find' -require_relative '../lib/const' -require_relative '../lib/color' - -CUR_DIR = File.dirname(__FILE__) - -# Add >LOCAL< lib to LOAD_PATH -$LOAD_PATH.unshift "#{CUR_DIR}/../lib" - -puts "Running tests..." - -Find.find(CUR_DIR) do |filename| - if File.extname(filename) == '.rb' - puts "\nTest Name: #{File.basename(filename, ".rb")}" - load filename - end -end - -puts "\nAll tests successful.".lightgreen