Commit 427ba9bc authored by Kazushi (Jam) Marukawa's avatar Kazushi (Jam) Marukawa Committed by GitHub

Merge pull request #963 from yancouto/cycle_test

Test for dependency cycle
parents 64748405 5f8da72e
## Creating tests
To create a test, just create a `.rb` file. It should finish normally if the test passed, and end with `exit 1` otherwise, preferably explaining why the test failed.
## Running tests
Just run the `test_all` file. Running tests manually may not work!
require 'find'
require_relative '../lib/color'
@all_pkgs = {}
# 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
end
# Looking for cycles. @path will keep the current dependency path.
# @state will store :on_path for vertices on the current dependency path
# and :visited for vertices that have already been checked not to lead to
# cycles.
@state = {}
@path = []
def dfs(pkg)
@path.push(pkg.name)
if @state[pkg] == :on_path
puts "Found dependency cycle!".lightred
while @path.first != @path.last
@path.shift
end
puts @path.to_s
exit 1
elsif @state[pkg] == nil
@state[pkg] = :on_path
if pkg.dependencies
pkg.dependencies.each do |name, v|
if name != pkg.name
dfs(@all_pkgs[name])
end
end
end
@state[pkg] = :visited
end
@path.pop
end
# Calls dfs for every path
@all_pkgs.each do |name, pkg|
dfs(pkg)
end
puts "No dependency cycles found.".lightgreen
#!/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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment