Commit ea979db

mo khan <mo@mokhan.ca>
2021-05-10 03:17:31
feat: detect `.terraform.lock.hcl` files
1 parent 6c10033
Changed files (2)
lib
spandx
terraform
spec
unit
terraform
lib/spandx/terraform/parsers/lock_file.rb
@@ -3,14 +3,25 @@
 module Spandx
   module Terraform
     module Parsers
-      class LockFile
-        def initialize; end
+      class LockFile < ::Spandx::Core::Parser
+        def initialize
+          @parser = Spandx::Terraform::Parsers::Hcl.new
+        end
+
+        def match?(pathname)
+          basename = pathname.basename
+          basename.fnmatch?('.terraform.lock.hcl')
+        end
 
         def parse(path)
-          parser = Spandx::Terraform::Parsers::Hcl.new
-          tree = parser.parse(IO.read(path))
-          puts tree.inspect
-          []
+          tree = @parser.parse(path.read)
+          tree[:blocks].map do |block|
+            ::Spandx::Core::Dependency.new(
+              name: block[:name],
+              version: block[:arguments].find { |x| x[:name] == 'version' }[:value],
+              path: path
+            )
+          end
         end
       end
     end
spec/unit/terraform/parsers/lock_file_spec.rb
@@ -3,6 +3,12 @@
 RSpec.describe Spandx::Terraform::Parsers::LockFile do
   subject(:parser) { described_class.new }
 
+  describe '#match?' do
+    it { is_expected.to be_match(to_path('.terraform.lock.hcl')) }
+    it { is_expected.not_to be_match(to_path('main.hcl')) }
+    it { is_expected.not_to be_match(to_path('main.tf')) }
+  end
+
   describe '#parse' do
     def build(name, version, path)
       Spandx::Core::Dependency.new(name: name, version: version, path: path)
@@ -13,7 +19,11 @@ RSpec.describe Spandx::Terraform::Parsers::LockFile do
 
       let(:path) { fixture_file('terraform/simple/.terraform.lock.hcl') }
 
-      specify { expect(subject).to match_array([build('registry.terraform.io/hashicorp/aws', '0.39.0', path)]) }
+      specify do
+        expect(subject).to match_array([
+          build('registry.terraform.io/hashicorp/aws', '3.39.0', path)
+        ])
+      end
     end
   end
 end