diff options
| author | mo khan <mo@mokhan.ca> | 2021-05-09 21:17:31 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2021-05-09 21:17:31 -0600 |
| commit | ea979dbb9090dc4eedb685eef6728f13fa124fa5 (patch) | |
| tree | 04770d97460de747d180fe501d899d1a6f1ee221 | |
| parent | 6c100339886de690be1fa20d30acb685f00a2484 (diff) | |
feat: detect `.terraform.lock.hcl` files
| -rw-r--r-- | lib/spandx/terraform/parsers/lock_file.rb | 23 | ||||
| -rw-r--r-- | spec/unit/terraform/parsers/lock_file_spec.rb | 12 |
2 files changed, 28 insertions, 7 deletions
diff --git a/lib/spandx/terraform/parsers/lock_file.rb b/lib/spandx/terraform/parsers/lock_file.rb index 9f0260c..5d86a66 100644 --- a/lib/spandx/terraform/parsers/lock_file.rb +++ b/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 diff --git a/spec/unit/terraform/parsers/lock_file_spec.rb b/spec/unit/terraform/parsers/lock_file_spec.rb index 0d7c10d..56eecd0 100644 --- a/spec/unit/terraform/parsers/lock_file_spec.rb +++ b/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 |
