diff --git a/npm_and_yarn/lib/dependabot/npm_and_yarn/file_fetcher.rb b/npm_and_yarn/lib/dependabot/npm_and_yarn/file_fetcher.rb index a066c5527c..6cbeb14509 100644 --- a/npm_and_yarn/lib/dependabot/npm_and_yarn/file_fetcher.rb +++ b/npm_and_yarn/lib/dependabot/npm_and_yarn/file_fetcher.rb @@ -645,8 +645,8 @@ def parsed_shrinkwrap def parsed_pnpm_workspace_yaml return {} unless pnpm_workspace_yaml - YAML.safe_load(T.must(T.must(pnpm_workspace_yaml).content)) - rescue Psych::SyntaxError + YAML.safe_load(T.must(T.must(pnpm_workspace_yaml).content), aliases: true) + rescue Psych::SyntaxError, Psych::BadAlias raise Dependabot::DependencyFileNotParseable, T.must(pnpm_workspace_yaml).path end diff --git a/npm_and_yarn/spec/dependabot/npm_and_yarn/file_fetcher_spec.rb b/npm_and_yarn/spec/dependabot/npm_and_yarn/file_fetcher_spec.rb index 3756b2c9c5..b3939dd784 100644 --- a/npm_and_yarn/spec/dependabot/npm_and_yarn/file_fetcher_spec.rb +++ b/npm_and_yarn/spec/dependabot/npm_and_yarn/file_fetcher_spec.rb @@ -2021,6 +2021,61 @@ end end + context "with a pnpm_workspace_yaml" do + let(:source) do + Dependabot::Source.new( + provider: "github", + repo: "gocardless/bump", + directory: "/" + ) + end + let(:file_fetcher) { described_class.new(source: source, credentials: credentials) } + let(:pnpm_workspace_yaml) { Dependabot::DependencyFile.new(name: "pnpm-workspace.yaml", content: content) } + + before do + allow(file_fetcher).to receive(:pnpm_workspace_yaml).and_return(pnpm_workspace_yaml) + end + + context "when it's content is nil" do + let(:pnpm_workspace_yaml) { nil } + + it "returns an empty hash" do + expect(file_fetcher.send(:parsed_pnpm_workspace_yaml)).to eq({}) + end + end + + context "when it's content is valid YAML" do + let(:content) { "---\npackages:\n - 'packages/*'\n" } + + it "parses the YAML content" do + expect(file_fetcher.send(:parsed_pnpm_workspace_yaml)).to eq({ "packages" => ["packages/*"] }) + end + end + + context "when it's content contains valid alias" do + let(:content) { "---\npackages:\n - &default 'packages/*'\n - *default\n" } + let(:pnpm_workspace_yaml) { Dependabot::DependencyFile.new(name: "pnpm-workspace.yaml", content: content) } + + it "parses the YAML content with aliases" do + expect(file_fetcher.send(:parsed_pnpm_workspace_yaml)).to eq({ "packages" => ["packages/*", "packages/*"] }) + end + end + + context "when it's content contains invalid alias (BadAlias)" do + let(:content) { "---\npackages:\n - &id 'packages/*'\n - *id" } # Invalid alias reference + + before do + allow(YAML).to receive(:safe_load).and_raise(Psych::BadAlias) + end + + it "raises a DependencyFileNotParseable error" do + expect do + file_fetcher.send(:parsed_pnpm_workspace_yaml) + end.to raise_error(Dependabot::DependencyFileNotParseable) + end + end + end + context "with package.json file just including a dummy string" do before do allow(file_fetcher_instance).to receive(:commit).and_return("sha")