deriving_license 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'deriving_license'
3
- s.version = '0.1.5'
3
+ s.version = '0.1.6'
4
4
  s.summary = "Deriving Licence finds the license agreements for all gems in your Gemfile"
5
5
  s.description = "Deriving Licence finds the license agreements for all gems in your Gemfile if included in your project, or in a Gemfile passed to the included binary"
6
6
  s.authors = ["Tom Allen"]
@@ -24,6 +24,13 @@ class DerivingLicense
24
24
  "beer" => "beerware",
25
25
  "ruby" => "Ruby"
26
26
  }
27
+
28
+ # String array of strategies to detect licenses. Write new class functions
29
+ # (that take a string of the dependency's name) then add their names here in
30
+ # order of fastest to slowest.
31
+ @@strategies = [
32
+ "from_gem_specification"
33
+ ]
27
34
 
28
35
  def self.run(path=nil)
29
36
  unless path
@@ -42,19 +49,20 @@ class DerivingLicense
42
49
 
43
50
  gemfile = Gemnasium::Parser::Gemfile.new(content)
44
51
 
45
- licenses = Hash.new(0)
52
+ detected_licenses = Hash.new(0)
53
+
54
+ # For each dependency specified...
46
55
  gemfile.dependencies.each do |d|
47
- # See if it's installed locally, and if not add -r to call
48
- Bundler.with_clean_env do # This gets out of the bundler context.
49
- remote = /#{d.name}/.match( `BUNDLE_GEMFILE=#{path}; gem list #{d.name}` ) ? "" : "-r "
50
- print "Determining license for #{d.name}#{remote.empty? ? "" : " (remote call required)"}..."
51
- yaml = `gem specification #{remote}#{d.name} --yaml`
52
- @spec = YAML.load(yaml, :safe => true)
56
+ print "Determining license for #{d.name}..."
57
+ # Try each license finding strategy...
58
+ @@strategies.each do |s|
59
+ @licenses = eval("#{s}(\"#{d.name}\")")
60
+ break if @licenses # and break out of the search if successful
53
61
  end
54
- print "#{@spec["licenses"].empty? ? "UNKNOWN" : "SUCCESS"}\n"
55
- @spec["licenses"].each{ |l| licenses[l]+=1 }
62
+ @licenses.each{ |l| detected_licenses[l]+=1 } # add each detected license to the results
63
+ print "DONE\n"
56
64
  end
57
- licenses
65
+ detected_licenses
58
66
  end
59
67
 
60
68
  def self.describe(licenses)
@@ -81,4 +89,17 @@ class DerivingLicense
81
89
  puts "There #{unknowns.count==1 ? "is" : "are"} also #{unknowns.count} unknown license#{unknowns.count==1 ? "" : "s"}: #{unknowns.join(', ')}"
82
90
  end
83
91
  end
92
+
93
+ ##############
94
+ # STRATEGIES #
95
+ ##############
96
+ def self.from_gem_specification(dep)
97
+ # See if the gem is installed locally, and if not add -r to call
98
+ Bundler.with_clean_env do # This gets out of the bundler context.
99
+ remote = /#{dep}/.match( `gem list #{dep}` ) ? "" : "-r "
100
+ yaml = `gem specification #{remote}#{dep} --yaml`
101
+ @spec = YAML.load(yaml, :safe => true)
102
+ end
103
+ @spec["licenses"]
104
+ end
84
105
  end
@@ -1,6 +1,19 @@
1
1
  require 'test/unit'
2
2
  require 'deriving_license'
3
3
 
4
+ # Monkey-patch stdout to test puts calls
5
+ require 'stringio'
6
+ module Kernel
7
+ def capture_stdout
8
+ out = StringIO.new
9
+ $stdout = out
10
+ yield
11
+ return out
12
+ ensure
13
+ $stdout = STDOUT
14
+ end
15
+ end
16
+
4
17
  class DerivingLicenseTest < Test::Unit::TestCase
5
18
  def test_run_throws_with_no_args
6
19
  assert_raise ArgumentError do
@@ -32,5 +45,22 @@ class DerivingLicenseTest < Test::Unit::TestCase
32
45
  end
33
46
  assert_equal( {"MIT"=>1}, DerivingLicense.run("Gemfile") )
34
47
  end
48
+
49
+ def test_describe_with_known_license
50
+ output = capture_stdout do
51
+ DerivingLicense.describe({"MIT" => 1})
52
+ end
53
+ assert_equal( false, /Detected/.match( output.string ).nil? )
54
+ end
55
+
56
+ def test_describe_with_unknown_license
57
+ output = capture_stdout do
58
+ DerivingLicense.describe({"Cheese" => 1})
59
+ end
60
+ # Shouldn't say "detected"
61
+ assert_equal( true, /Detected/.match( output.string ).nil? )
62
+ # Should say "unknown"
63
+ assert_equal( false, /unknown/.match( output.string ).nil? )
64
+ end
35
65
 
36
66
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deriving_license
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-30 00:00:00.000000000 Z
12
+ date: 2013-05-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: gemnasium-parser
@@ -81,9 +81,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  version: '0'
82
82
  requirements: []
83
83
  rubyforge_project:
84
- rubygems_version: 1.8.24
84
+ rubygems_version: 1.8.23
85
85
  signing_key:
86
86
  specification_version: 3
87
87
  summary: Deriving Licence finds the license agreements for all gems in your Gemfile
88
88
  test_files:
89
89
  - test/test_deriving_license.rb
90
+ has_rdoc: