Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Properly handle ~ artifact targets. #9531

Merged
merged 1 commit into from Dec 15, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 11 additions & 10 deletions Library/Homebrew/cask/artifact/artifact.rb
@@ -1,4 +1,4 @@
# typed: false
# typed: true
# frozen_string_literal: true

require "cask/artifact/moved"
Expand All @@ -19,25 +19,26 @@ def self.english_name
"Generic Artifact"
end

sig { params(cask: Cask, args: T.untyped).returns(T.attached_class) }
def self.from_args(cask, *args)
source_string, target_hash = args
source, options = args

raise CaskInvalidError.new(cask.token, "no source given for #{english_name}") if source_string.nil?
raise CaskInvalidError.new(cask.token, "No source provided for #{english_name}.") if source.blank?

unless target_hash.is_a?(Hash)
raise CaskInvalidError.new(cask.token, "target required for #{english_name} '#{source_string}'")
unless options.try(:key?, :target)
raise CaskInvalidError.new(cask.token, "#{english_name} '#{source}' requires a target.")
end

target_hash.assert_valid_keys!(:target)

new(cask, source_string, **target_hash)
new(cask, source, **options)
end

sig { params(target: T.any(String, Pathname)).returns(Pathname) }
def resolve_target(target)
Pathname(target)
super(target, base_dir: nil)
end

def initialize(cask, source, target: nil)
sig { params(cask: Cask, source: T.any(String, Pathname), target: T.any(String, Pathname)).void }
def initialize(cask, source, target:)
super(cask, source, target: target)
end
end
Expand Down
15 changes: 13 additions & 2 deletions Library/Homebrew/cask/artifact/relocated.rb
Expand Up @@ -28,12 +28,23 @@ def self.from_args(cask, *args)
new(cask, source_string, **target_hash)
end

def resolve_target(target)
config.public_send(self.class.dirmethod).join(target)
def resolve_target(target, base_dir: config.public_send(self.class.dirmethod))
target = Pathname(target)

if target.relative?
return target.expand_path if target.descend.first.to_s == "~"
return base_dir/target if base_dir
end

target
end

attr_reader :source, :target

sig do
params(cask: Cask, source: T.nilable(T.any(String, Pathname)), target: T.nilable(T.any(String, Pathname)))
.void
end
def initialize(cask, source, target: nil)
super(cask)

Expand Down
2 changes: 2 additions & 0 deletions Library/Homebrew/cask/cask.rbi
Expand Up @@ -5,5 +5,7 @@ module Cask
def artifacts; end

def homepage; end

def staged_path; end
end
end
18 changes: 17 additions & 1 deletion Library/Homebrew/test/cask/artifact/generic_artifact_spec.rb
Expand Up @@ -23,7 +23,23 @@
it "fails to load" do
expect {
Cask::CaskLoader.load(cask_path("invalid/invalid-generic-artifact-no-target"))
}.to raise_error(Cask::CaskInvalidError, /target required for Generic Artifact/)
}.to raise_error(Cask::CaskInvalidError, /Generic Artifact.*requires.*target/)
end
end

context "with relative target" do
it "does not fail to load" do
expect {
Cask::CaskLoader.load(cask_path("generic-artifact-relative-target"))
}.not_to raise_error
end
end

context "with user-relative target" do
it "does not fail to load" do
expect {
Cask::CaskLoader.load(cask_path("generic-artifact-user-relative-target"))
}.not_to raise_error
end
end

Expand Down
10 changes: 8 additions & 2 deletions Library/Homebrew/test/cask/audit_spec.rb
Expand Up @@ -756,13 +756,19 @@ def tmp_cask(name, text)
context "with relative target" do
let(:cask_token) { "generic-artifact-relative-target" }

it { is_expected.to fail_with(/target must be absolute path for Generic Artifact/) }
it { is_expected.to fail_with(/target must be.*absolute/) }
end

context "with user-relative target" do
let(:cask_token) { "generic-artifact-user-relative-target" }

it { is_expected.not_to fail_with(/target must be.*absolute/) }
end

context "with absolute target" do
let(:cask_token) { "generic-artifact-absolute-target" }

it { is_expected.not_to fail_with(/target required for Generic Artifact/) }
it { is_expected.not_to fail_with(/target must be.*absolute/) }
end
end

Expand Down
@@ -0,0 +1,11 @@
cask "generic-artifact-user-relative-target" do
version "1.2.3"
sha256 "d5b2dfbef7ea28c25f7a77cd7fa14d013d82b626db1d82e00e25822464ba19e2"

url "file://#{TEST_FIXTURE_DIR}/cask/AppWithBinary.zip"
name "With Binary"
desc "Cask with a binary stanza"
homepage "https://brew.sh/with-binary"

artifact "Caffeine.app", target: "~/Desktop/Caffeine.app"
end