deriving_license 0.2.9 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gemspec
3
+ gemspec :name => "deriving_license"
4
4
 
5
5
  # NB: For convenient testing, it's worth keeping this here.
6
6
  group :test do
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'deriving_license'
3
- s.version = '0.2.9'
3
+ s.version = '0.3.0'
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"]
@@ -64,23 +64,44 @@ class DerivingLicense
64
64
  end unless strategies.nil?
65
65
  available_strategies = strategies.nil? ? @strategies : strategies
66
66
 
67
- if /(gemspec)+/.match(path.downcase)
68
- content = Gemnasium::Parser::Gemspec.new(content)
69
- else
67
+ gemspec_content = ""
68
+ deps = []
69
+ if /gemspec/.match(path.downcase) # Gemspecs...
70
+ deps = Gemnasium::Parser::Gemspec.new(content).dependencies
71
+ else # Gemfiles...
70
72
  content = Gemnasium::Parser::Gemfile.new(content)
73
+ if content.gemspec? # If the gemfile sources a gemspec.
74
+ begin
75
+ gemspec_content = File.open(content.gemspec, "r").read
76
+ rescue # If we fail to read the gemspec, search current directory
77
+ possible_gemspec_files = Find.find( File.dirname(content.gemspec) )
78
+ if possible_gemspec_files.count == 1
79
+ begin
80
+ gemspec_content = File.open(possible_gemspec_files[0], "r").read
81
+ rescue
82
+ raise "Could not open gemspec file \"#{possible_gemspec_files[0]}\" that was called by gemfile."
83
+ end
84
+ else
85
+ raise "Gemfile calls out to unnamed gemspec, but multiple gemspec files found in gemfile's directory. Tell the developer to name their sources!"
86
+ end
87
+ end
88
+ end
89
+ deps = content.dependencies
90
+ deps += Gemnasium::Parser::Gemspec.new(gemspec_content).dependencies unless gemspec_content.empty?
71
91
  end
72
92
  detected_licenses = Hash.new(0)
73
93
  detected_licenses["custom"]=[]
74
94
 
75
95
  # For each dependency specified...
76
- content.dependencies.each do |d|
96
+ licenses = []
97
+ deps.each do |d|
77
98
  print "Determining license for #{d.name}:\n"
78
99
  # Try each license finding strategy...
79
100
  available_strategies.each do |s|
80
101
  print "\tTrying #{s} strategy..."
81
- @licenses = eval("#{s}(\"#{d.name}\")")
82
- unless @licenses.empty? # and break out of the search if successful
83
- if @licenses.count == 1 and @licenses[0] == "custom"
102
+ licenses = eval("#{s}(\"#{d.name}\")")
103
+ unless licenses.empty? # and break out of the search if successful
104
+ if licenses.count == 1 and licenses[0] == "custom"
84
105
  print "CUSTOM\n"
85
106
  else
86
107
  print "SUCCESS\n"
@@ -89,7 +110,7 @@ class DerivingLicense
89
110
  end
90
111
  print "FAILED\n"
91
112
  end
92
- @licenses.each do |l| # add each detected license to the results
113
+ licenses.each do |l| # add each detected license to the results
93
114
  unless l == "custom"
94
115
  detected_licenses[l]+=1
95
116
  else
@@ -136,16 +157,16 @@ class DerivingLicense
136
157
 
137
158
  def self.get_gem_spec(dep)
138
159
  # Check spec cache first.
139
- @spec = @specs_cache[dep]
140
- return @spec if @spec
160
+ spec = @specs_cache[dep]
161
+ return spec if spec
141
162
  # See if the gem is installed locally, and if not add -r to call
142
163
  Bundler.with_clean_env do # This gets out of the bundler context.
143
164
  remote = /#{dep}/.match( `gem list #{dep}` ) ? "" : "-r "
144
165
  yaml = `gem specification #{remote}#{dep} --yaml`
145
- @spec = YAML.load(yaml, :safe => true)
166
+ spec = YAML.load(yaml, :safe => true)
146
167
  end
147
- @specs_cache[dep] = @spec # Cache it.
148
- @spec
168
+ @specs_cache[dep] = spec # Cache it.
169
+ spec
149
170
  end
150
171
 
151
172
  def self.yield_gem_source_directory(dep)
@@ -41,21 +41,23 @@ class DerivingLicenseTest < Test::Unit::TestCase
41
41
  end
42
42
 
43
43
  def test_run_with_valid_gemfile_arg
44
+ result = {}
44
45
  assert_nothing_raised do
45
46
  output = capture_stdout do
46
- @result = DerivingLicense.run("Gemfile")
47
+ result = DerivingLicense.run("Gemfile")
47
48
  end
48
49
  end
49
- assert_equal( true, @result.has_key?("MIT") )
50
+ assert_equal( true, result.has_key?("MIT") )
50
51
  end
51
52
 
52
53
  def test_run_with_valid_gemspec_arg
54
+ result = {}
53
55
  assert_nothing_raised do
54
56
  output = capture_stdout do
55
- @result = DerivingLicense.run("deriving_license.gemspec")
57
+ result = DerivingLicense.run("deriving_license.gemspec")
56
58
  end
57
59
  end
58
- assert_equal( true, @result.has_key?("MIT") )
60
+ assert_equal( true, result.has_key?("MIT") )
59
61
  end
60
62
 
61
63
  def test_run_with_non_existant_strategies
@@ -91,51 +93,72 @@ class DerivingLicenseTest < Test::Unit::TestCase
91
93
  end
92
94
 
93
95
  def test_from_scraping_strategy
96
+ result = {}
94
97
  output = capture_stdout do
95
- @result = DerivingLicense.run("./test/requires_scraping.gemfile", ["from_scraping_homepage"])
98
+ result = DerivingLicense.run("./test/requires_scraping.gemfile", ["from_scraping_homepage"])
96
99
  end
97
- assert_equal( false, @result.empty? )
100
+ assert_equal( false, result.empty? )
98
101
  assert_equal( false, /from_scraping_homepage strategy...SUCCESS/.match( output.string ).nil? ) # Should be SUCCESS
99
102
  end
100
103
 
101
104
  def test_from_scraping_strategy_with_invalid_homepage
105
+ result = {}
102
106
  output = capture_stdout do
103
- @result = DerivingLicense.run("./test/requires_scraping_but_invalid_homepage.gemfile", ["from_scraping_homepage"])
107
+ result = DerivingLicense.run("./test/requires_scraping_but_invalid_homepage.gemfile", ["from_scraping_homepage"])
104
108
  end
105
- assert_equal( true, @result.empty? )
109
+ assert_equal( true, result.empty? )
106
110
  assert_equal( false, /from_scraping_homepage strategy...FAILED/.match( output.string ).nil? ) # Should be FAILED
107
111
  end
108
112
 
109
113
  def test_from_license_filename
114
+ result = {}
110
115
  output = capture_stdout do
111
- @result = DerivingLicense.run("./test/requires_license_filename.gemfile", ["from_license_file"])
116
+ result = DerivingLicense.run("./test/requires_license_filename.gemfile", ["from_license_file"])
112
117
  end
113
- assert_equal( false, @result.empty? )
118
+ assert_equal( false, result.empty? )
114
119
  assert_equal( true, /from_license_file strategy...FAILED/.match( output.string ).nil? ) # Shouldn't be FAILED
115
120
  end
116
121
 
117
122
  def test_from_license_file_parsing
123
+ result = {}
118
124
  output = capture_stdout do
119
- @result = DerivingLicense.run("./test/requires_license_file_parsing.gemfile", ["from_license_file"])
125
+ result = DerivingLicense.run("./test/requires_license_file_parsing.gemfile", ["from_license_file"])
120
126
  end
121
- assert_equal( false, @result.empty? )
127
+ assert_equal( false, result.empty? )
122
128
  assert_equal( true, /from_license_file strategy...FAILED/.match( output.string ).nil? ) # Shouldn't be FAILED
123
129
  end
124
130
 
125
131
  def test_from_license_file_parsing_but_is_custom
132
+ result = {}
126
133
  output = capture_stdout do
127
- @result = DerivingLicense.run("./test/requires_license_file_parsing_but_is_custom.gemfile", ["from_license_file"])
134
+ result = DerivingLicense.run("./test/requires_license_file_parsing_but_is_custom.gemfile", ["from_license_file"])
128
135
  end
129
- assert_equal( false, @result.empty? )
136
+ assert_equal( false, result.empty? )
130
137
  assert_equal( false, /from_license_file strategy...CUSTOM/.match( output.string ).nil? ) # Should be CUSTOM
131
138
  end
132
139
 
133
140
  def test_from_readme_file_parsing
141
+ result = {}
134
142
  output = capture_stdout do
135
- @result = DerivingLicense.run("./test/requires_readme_file_parsing.gemfile", ["from_parsing_readme"])
143
+ result = DerivingLicense.run("./test/requires_readme_file_parsing.gemfile", ["from_parsing_readme"])
136
144
  end
137
- assert_equal( false, @result.empty? )
145
+ assert_equal( false, result.empty? )
138
146
  assert_equal( false, /from_parsing_readme strategy...SUCCESS/.match( output.string ).nil? )
139
147
  end
140
148
 
149
+ def test_gemfile_with_call_to_gemspec
150
+ gemfileResult = {}
151
+ gemspecResult = {}
152
+ output = capture_stdout do
153
+ gemfileResult = DerivingLicense.run("Gemfile")
154
+ gemspecResult = DerivingLicense.run("deriving_license.gemspec")
155
+ end
156
+ gemspecResult.each do |k,v|
157
+ # Each license found in the gemspec should be found in the gemfile too
158
+ # (because the gemfile includes the gemspec, so it's a superset.)
159
+ assert_equal( true, gemfileResult.has_key?(k) )
160
+ end
161
+ assert_equal( true, gemfileResult.count >= gemspecResult.count )
162
+ end
163
+
141
164
  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.2.9
4
+ version: 0.3.0
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-05-06 00:00:00.000000000 Z
12
+ date: 2013-05-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: gemnasium-parser