Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Enabling alias parsing in pnpm_workspace_yaml #11353

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions npm_and_yarn/lib/dependabot/npm_and_yarn/file_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
thavaahariharangit marked this conversation as resolved.
Show resolved Hide resolved
rescue Psych::SyntaxError, Psych::BadAlias
raise Dependabot::DependencyFileNotParseable, T.must(pnpm_workspace_yaml).path
end

Expand Down
55 changes: 55 additions & 0 deletions npm_and_yarn/spec/dependabot/npm_and_yarn/file_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading