From 1fc1c82aeddbaa172e7b7b6f22cd88e69015cbbb Mon Sep 17 00:00:00 2001 From: Luca Ongaro Date: Wed, 25 Jan 2023 00:08:15 +0100 Subject: [PATCH] Fix code style and add tests --- .../cask/artifact/abstract_artifact.rb | 6 ++-- .../cask/artifact/abstract_artifact_spec.rb | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 Library/Homebrew/test/cask/artifact/abstract_artifact_spec.rb diff --git a/Library/Homebrew/cask/artifact/abstract_artifact.rb b/Library/Homebrew/cask/artifact/abstract_artifact.rb index 047093c357..14d203ee4b 100644 --- a/Library/Homebrew/cask/artifact/abstract_artifact.rb +++ b/Library/Homebrew/cask/artifact/abstract_artifact.rb @@ -99,11 +99,11 @@ module Cask description = key ? "#{stanza} #{key.inspect}" : stanza.to_s # backward-compatible string value - if arguments.is_a?(String) - arguments = { executable: arguments } + arguments = if arguments.is_a?(String) + { executable: arguments } else # Avoid mutating the original argument - arguments = arguments.dup + arguments.dup end # key sanity diff --git a/Library/Homebrew/test/cask/artifact/abstract_artifact_spec.rb b/Library/Homebrew/test/cask/artifact/abstract_artifact_spec.rb new file mode 100644 index 0000000000..1c86058b11 --- /dev/null +++ b/Library/Homebrew/test/cask/artifact/abstract_artifact_spec.rb @@ -0,0 +1,30 @@ +# typed: false +# frozen_string_literal: true + +describe Cask::Artifact::AbstractArtifact, :cask do + describe ".read_script_arguments" do + it "accepts a string, and uses it as the executable" do + arguments = "something" + stanza = :installer + + expect(described_class.read_script_arguments(arguments, stanza)).to eq(["something", {}]) + end + + it "accepts a hash with an executable" do + arguments = { executable: "something" } + stanza = :installer + + expect(described_class.read_script_arguments(arguments, stanza)).to eq(["something", {}]) + end + + it "does not mutate the arguments in place" do + arguments = { executable: "something", foo: "bar" } + clone = arguments.dup + stanza = :installer + + described_class.read_script_arguments(arguments, stanza) + + expect(arguments).to eq(clone) + end + end +end