Skip to content
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
33 changes: 31 additions & 2 deletions scripts/homebrew_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@ def assert_equal(expected, actual)
raise "Expected #{expected.inspect}, got #{actual.inspect}" unless expected == actual
end

def assert_install_generates_completions(formula, directory)
# -- Arrange --
prefix = Pathname(directory) / 'prefix'
formula.define_singleton_method(:prefix) { prefix }
source = Pathname(directory) / 'apple-docs-test'
source.write <<~SH
#!/bin/sh
test "$1" = '--generate-completion-script' || exit 1
printf 'completion for %s\\n' "$2"
SH
source.chmod 0644

# -- Act --
Dir.chdir(directory) { formula.install }

# -- Assert --
raise 'Installed binary is not executable' unless (formula.bin / 'apple-docs').executable?

assert_equal("completion for bash\n", (formula.bash_completion / 'apple-docs').read)
assert_equal("completion for zsh\n", (formula.zsh_completion / '_apple-docs').read)
assert_equal("completion for fish\n", (formula.fish_completion / 'apple-docs.fish').read)
end

def assert_rejected
yield
rescue ArgumentError
Expand All @@ -33,7 +56,7 @@ def assert_rejected
template_path = File.join(root, 'templates', "#{name}.rb")
template = File.read(template_path)

%w[0.0.3 0.0.4 0.0.5].each do |version|
%w[0.0.3 0.0.4 0.0.5 0.0.6].each do |version|
values = checksums.merge('VERSION' => version)
rendered = render_homebrew(template, values)
raise 'Unresolved template placeholder' if rendered.match?(/\{\{.*?\}\}/)
Expand All @@ -52,7 +75,7 @@ def assert_rejected
Homebrew::SimulateSystem.with(os: os, arch: arch) do
path = Pathname(directory) / version / platform / "#{name}.rb"
formula = Formulary.from_contents(name, path, rendered, tap: Tap.fetch('techprimate/publisher'))
expected_revision = name == 'apple-docs' && version == '0.0.4' ? 1 : 0
expected_revision = name == 'apple-docs' ? { '0.0.4' => 1, '0.0.5' => 1 }.fetch(version, 0) : 0
assert_equal(version, formula.version.to_s)
assert_equal(expected_revision, formula.revision)
assert_equal(
Expand All @@ -61,6 +84,12 @@ def assert_rejected
)
assert_equal(checksums.fetch("SHA_#{platform.upcase.tr('-', '_')}"), formula.stable.checksum.to_s)
raise 'Missing formula test' unless formula.test_defined?

if name == 'apple-docs' && version == '0.0.5'
Dir.mktmpdir('install-', directory) do |install_directory|
assert_install_generates_completions(formula, install_directory)
end
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion templates/apple-docs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class AppleDocs < Formula
homepage 'https://github.com/techprimate/apple-docs-cli'
version '{{VERSION}}'
license 'FSL-1.1-MIT'
revision 1 if version.to_s == '0.0.4'
revision 1 if %w[0.0.4 0.0.5].include?(version.to_s)

on_macos do
depends_on macos: :ventura
Expand Down Expand Up @@ -32,6 +32,7 @@ class AppleDocs < Formula
def install
binary = Dir['apple-docs-*'].first
bin.install binary => 'apple-docs'
(bin / 'apple-docs').chmod 0755
generate_completions_from_executable(bin / 'apple-docs', '--generate-completion-script')
end

Expand Down