Merge pull request #13 from github/test-source

Command source-agnostic tests and integration tests
This commit is contained in:
Jon Ruskin 2018-03-28 20:09:08 -07:00 коммит произвёл GitHub
Родитель b7e8e3b454 c8bf943ffa
Коммит 933f9d7932
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 175 добавлений и 67 удалений

Просмотреть файл

@ -3,34 +3,57 @@ require "test_helper"
describe Licensed::Command::Cache do
let(:config) { Licensed::Configuration.new }
let(:source) { TestSource.new }
let(:generator) { Licensed::Command::Cache.new(config) }
let(:fixtures) { File.expand_path("../../fixtures", __FILE__) }
before do
config.ui.level = "silent"
FileUtils.rm_rf config.cache_path
config.apps.each do |app|
app.sources.clear
app.sources << source
end
end
it "extracts license info for each ruby dep" do
generator.run
assert config.cache_path.join("rubygem/licensee.txt").exist?
license = Licensed::License.read(config.cache_path.join("rubygem/licensee.txt"))
assert_equal "licensee", license["name"]
assert_equal "mit", license["license"]
after do
config.apps.each do |app|
FileUtils.rm_rf app.cache_path
end
end
each_source do |source_type|
describe "with #{source_type}" do
let(:yaml) { YAML.load_file(File.join(fixtures, "command/#{source_type.to_s.downcase}.yml")) }
let(:expected_dependency) { yaml["expected_dependency"] }
let(:config) { Licensed::Configuration.new(yaml["config"]) }
let(:source) { Licensed::Source.const_get(source_type).new(config) }
it "extracts license info" do
generator.run
path = config.cache_path.join("#{source.type}/#{expected_dependency}.txt")
assert path.exist?
license = Licensed::License.read(path)
assert_equal expected_dependency, license["name"]
assert license["license"]
end
end
end
it "cleans up old dependencies" do
FileUtils.mkdir_p config.cache_path.join("rubygem")
File.write config.cache_path.join("rubygem/old_dep.txt"), ""
FileUtils.mkdir_p config.cache_path.join("test")
File.write config.cache_path.join("test/old_dep.txt"), ""
generator.run
refute config.cache_path.join("rubygem/old_dep.txt").exist?
refute config.cache_path.join("test/old_dep.txt").exist?
end
it "cleans up ignored dependencies" do
FileUtils.mkdir_p config.cache_path.join("rubygem")
File.write config.cache_path.join("rubygem/licensee.txt"), ""
config.ignore "type" => "rubygem", "name" => "licensee"
FileUtils.mkdir_p config.cache_path.join("test")
File.write config.cache_path.join("test/dependency.txt"), ""
config.ignore "type" => "test", "name" => "dependency"
generator.run
refute config.cache_path.join("rubygem/licensee.txt").exist?
refute config.cache_path.join("test/dependency.txt").exist?
end
it "does not include ignored dependencies in dependency counts" do
@ -38,9 +61,9 @@ describe Licensed::Command::Cache do
out, _ = capture_io { generator.run }
count = out.match(/dependencies: (\d+)/)[1].to_i
FileUtils.mkdir_p config.cache_path.join("rubygem")
File.write config.cache_path.join("rubygem/licensee.txt"), ""
config.ignore "type" => "rubygem", "name" => "licensee"
FileUtils.mkdir_p config.cache_path.join("test")
File.write config.cache_path.join("test/dependency.txt"), ""
config.ignore "type" => "test", "name" => "dependency"
out, _ = capture_io { generator.run }
ignored_count = out.match(/dependencies: (\d+)/)[1].to_i
@ -66,21 +89,17 @@ describe Licensed::Command::Cache do
it "caches metadata for all apps" do
generator.run
assert config["apps"][0].cache_path.join("rubygem/licensee.txt").exist?
assert config["apps"][1].cache_path.join("rubygem/licensee.txt").exist?
assert config["apps"][0].cache_path.join("test/dependency.txt").exist?
assert config["apps"][1].cache_path.join("test/dependency.txt").exist?
end
end
describe "with app.source_path" do
let(:fixtures) { File.expand_path("../../fixtures/npm", __FILE__) }
let(:config) { Licensed::Configuration.new("source_path" => fixtures) }
it "changes the current directory to app.source_path while running" do
config.stub(:enabled?, ->(type) { type == "npm" }) do
generator.run
end
assert config.cache_path.join("npm/autoprefixer.txt").exist?
source.dependencies_hook = -> { assert_equal fixtures, Dir.pwd }
generator.run
end
end
end

Просмотреть файл

@ -3,23 +3,41 @@ require "test_helper"
describe Licensed::Command::List do
let(:config) { Licensed::Configuration.new }
let(:source) { TestSource.new }
let(:command) { Licensed::Command::List.new(config) }
let(:fixtures) { File.expand_path("../../fixtures", __FILE__) }
it "lists dependencies for all source types" do
out, = capture_io { command.run }
config.sources.each do |s|
assert_match(/#{s.type} dependencies:/, out)
before do
config.apps.each do |app|
app.sources.clear
app.sources << source
end
end
each_source do |source_type|
describe "with #{source_type}" do
let(:yaml) { YAML.load_file(File.join(fixtures, "command/#{source_type.to_s.downcase}.yml")) }
let(:expected_dependency) { yaml["expected_dependency"] }
let(:config) { Licensed::Configuration.new(yaml["config"]) }
let(:source) { Licensed::Source.const_get(source_type).new(config) }
it "lists dependencies" do
out, = capture_io { command.run }
assert_match(/Found #{expected_dependency}/, out)
assert_match(/#{source.type} dependencies:/, out)
end
end
end
it "does not include ignored dependencies" do
out, = capture_io { command.run }
assert_match(/licensee/, out)
assert_match(/dependency/, out)
count = out.match(/dependencies: (\d+)/)[1].to_i
config.ignore("type" => "rubygem", "name" => "licensee")
config.ignore("type" => "test", "name" => "dependency")
out, = capture_io { command.run }
refute_match(/licensee/, out)
refute_match(/dependency/, out)
ignored_count = out.match(/dependencies: (\d+)/)[1].to_i
assert_equal count - 1, ignored_count
end
@ -50,15 +68,11 @@ describe Licensed::Command::List do
end
describe "with app.source_path" do
let(:fixtures) { File.expand_path("../../fixtures/npm", __FILE__) }
let(:config) { Licensed::Configuration.new("source_path" => fixtures) }
it "changes the current directory to app.source_path while running" do
out, = config.stub(:enabled?, ->(type) { type == "npm" }) do
capture_io { command.run }
end
assert_match(/Found autoprefixer/, out)
source.dependencies_hook = -> { assert_equal fixtures, Dir.pwd }
capture_io { command.run }
end
end
end

Просмотреть файл

@ -3,79 +3,91 @@ require "test_helper"
describe Licensed::Command::Status do
let(:config) { Licensed::Configuration.new }
let(:source) { TestSource.new }
let(:verifier) { Licensed::Command::Status.new(config) }
before do
config.apps.each do |app|
app.sources.clear
app.sources << source
end
config.ui.silence do
Licensed::Command::Cache.new(config.dup).run(force: true)
end
@verifier = Licensed::Command::Status.new(config)
end
after do
config.apps.each do |app|
FileUtils.rm_rf app.cache_path
end
end
it "warns if license is not allowed" do
out, _ = capture_io { @verifier.run }
out, _ = capture_io { verifier.run }
assert_match(/license needs reviewed: mit/, out)
end
it "does not warn if license is allowed" do
config.allow "mit"
out, _ = capture_io { @verifier.run }
out, _ = capture_io { verifier.run }
refute_match(/license needs reviewed: mit/, out)
end
it "does not warn if dependency is ignored" do
out, _ = capture_io { @verifier.run }
assert_match(/licensee.txt/, out)
out, _ = capture_io { verifier.run }
assert_match(/dependency.txt/, out)
config.ignore "type" => "rubygem", "name" => "licensee"
out, _ = capture_io { @verifier.run }
refute_match(/licensee.txt/, out)
config.ignore "type" => "test", "name" => "dependency"
out, _ = capture_io { verifier.run }
refute_match(/dependency.txt/, out)
end
it "does not warn if dependency is reviewed" do
out, _ = capture_io { @verifier.run }
assert_match(/licensee/, out)
out, _ = capture_io { verifier.run }
assert_match(/dependency/, out)
config.review "type" => "rubygem", "name" => "licensee"
out, _ = capture_io { @verifier.run }
refute_match(/licensee/, out)
config.review "type" => "test", "name" => "dependency"
out, _ = capture_io { verifier.run }
refute_match(/dependency/, out)
end
it "warns if license is empty" do
filename = config.cache_path.join("rubygem/licensee.txt")
filename = config.cache_path.join("test/dependency.txt")
license = Licensed::License.read(filename)
license.text = ""
license.save(filename)
out, _ = capture_io { @verifier.run }
out, _ = capture_io { verifier.run }
assert_match(/missing license text/, out)
end
it "warns if versions do not match" do
@verifier.app_dependencies(config.apps.first).first["version"] = "nope"
out, _ = capture_io { @verifier.run }
verifier.app_dependencies(config.apps.first).first["version"] = "nope"
out, _ = capture_io { verifier.run }
assert_match(/cached license data out of date/, out)
end
it "warns if cached license data missing" do
FileUtils.rm config.cache_path.join("rubygem/licensee.txt")
out, _ = capture_io { @verifier.run }
FileUtils.rm config.cache_path.join("test/dependency.txt")
out, _ = capture_io { verifier.run }
assert_match(/cached license data missing/, out)
end
it "does not warn if cached license data missing for ignored gem" do
FileUtils.rm config.cache_path.join("rubygem/licensee.txt")
config.ignore "type" => "rubygem", "name" => "licensee"
FileUtils.rm config.cache_path.join("test/dependency.txt")
config.ignore "type" => "test", "name" => "dependency"
out, _ = capture_io { @verifier.run }
refute_match(/licensee/, out)
out, _ = capture_io { verifier.run }
refute_match(/dependency/, out)
end
it "does not include ignored dependencies in dependency counts" do
out, _ = capture_io { @verifier.run }
out, _ = capture_io { verifier.run }
count = out.match(/(\d+) dependencies checked/)[1].to_i
config.ignore "type" => "rubygem", "name" => "licensee"
out, _ = capture_io { @verifier.run }
config.ignore "type" => "test", "name" => "dependency"
out, _ = capture_io { verifier.run }
ignored_count = out.match(/(\d+) dependencies checked/)[1].to_i
assert_equal count - 1, ignored_count
end
@ -98,7 +110,7 @@ describe Licensed::Command::Status do
let(:config) { Licensed::Configuration.new("apps" => apps) }
it "verifies dependencies for all apps" do
out, _ = capture_io { @verifier.run }
out, _ = capture_io { verifier.run }
config.apps.each do |app|
assert_match(/Checking licenses for #{app['name']}/, out)
end
@ -110,8 +122,8 @@ describe Licensed::Command::Status do
let(:config) { Licensed::Configuration.new("source_path" => fixtures) }
it "changes the current directory to app.source_path while running" do
out, _ = capture_io { @verifier.run }
assert_match(/autoprefixer.txt:\s+?- license needs reviewed/, out)
source.dependencies_hook = -> { assert_equal fixtures, Dir.pwd }
capture_io { verifier.run }
end
end
end

3
test/fixtures/command/bower.yml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,3 @@
expected_dependency: jquery
config:
source_path: test/fixtures/bower

3
test/fixtures/command/bundler.yml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,3 @@
expected_dependency: semantic
config:
source_path: test/fixtures/bundler

8
test/fixtures/command/cabal.yml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,8 @@
expected_dependency: text
config:
source_path: test/fixtures/haskell
cabal:
ghc_package_db:
- global
- test/fixtures/haskell/dist-newstyle/packagedb/ghc-<ghc_version>
- ~/.cabal/store/ghc-<ghc_version>/package.db

5
test/fixtures/command/go.yml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,5 @@
expected_dependency: github.com/gorilla/context
config:
source_path: test/fixtures/go/src/test
go:
GOPATH: test/fixtures/go

5
test/fixtures/command/manifest.yml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,5 @@
expected_dependency: manifest_test
config:
source_path: test/fixtures/manifest
manifest:
path: test/fixtures/manifest/manifest.yml

3
test/fixtures/command/npm.yml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,3 @@
expected_dependency: autoprefixer
config:
source_path: test/fixtures/npm

Просмотреть файл

@ -18,3 +18,39 @@ Minitest::Spec.class_eval do
Licensed.use_github = false
end
end
class TestSource
attr_accessor :dependencies_hook
def initialize
@dependencies_hook = nil
end
def type
"test"
end
def enabled?
true
end
def dependencies
@dependencies_hook.call if @dependencies_hook.respond_to?(:call)
@dependencies ||= [
Licensed::Dependency.new(Dir.pwd, {
"type" => type,
"name" => "dependency",
"version" => "1.0"
})
]
end
end
def each_source(&block)
Licensed::Source.constants.each do |source_type|
# if a specific source type is set via ENV, skip other source types
next if ENV["SOURCE"] && source_type.to_s.downcase != ENV["SOURCE"].downcase
block.call(source_type)
end
end