From f2fa2c5d3095438b4bb387d58c426ad91f2b1378 Mon Sep 17 00:00:00 2001 From: Frank Lam Date: Sun, 26 Apr 2020 21:13:19 +0800 Subject: [PATCH] Add JSON support to brew cask outdated * brew outdated already has JSON support, now users and tools can get similar results with brew cask outdated --json --- Library/Homebrew/cask/cmd/outdated.rb | 23 ++-- .../Homebrew/test/cask/cmd/outdated_spec.rb | 100 ++++++++++++++++++ 2 files changed, 107 insertions(+), 16 deletions(-) diff --git a/Library/Homebrew/cask/cmd/outdated.rb b/Library/Homebrew/cask/cmd/outdated.rb index f11bd6dd31..98611244da 100644 --- a/Library/Homebrew/cask/cmd/outdated.rb +++ b/Library/Homebrew/cask/cmd/outdated.rb @@ -5,30 +5,21 @@ module Cask class Outdated < AbstractCommand option "--greedy", :greedy, false option "--quiet", :quiet, false + option "--json", :json, false def initialize(*) super self.verbose = ($stdout.tty? || verbose?) && !quiet? + @outdated_casks = casks(alternative: -> { Caskroom.casks }).select do |cask| + odebug "Checking update info of Cask #{cask}" + cask.outdated?(greedy?) + end end def run - casks(alternative: -> { Caskroom.casks }).each do |cask| - odebug "Checking update info of Cask #{cask}" - self.class.list_if_outdated(cask, greedy?, verbose?) - end - end + output = @outdated_casks.map { |cask| cask.outdated_info(greedy?, verbose?, json?) } - def self.list_if_outdated(cask, greedy, verbose) - return unless cask.outdated?(greedy) - - if verbose - outdated_versions = cask.outdated_versions(greedy) - outdated_info = "#{cask.token} (#{outdated_versions.join(", ")})" - current_version = cask.version.to_s - puts "#{outdated_info} != #{current_version}" - else - puts cask.token - end + puts json? ? JSON.generate(output) : output end def self.help diff --git a/Library/Homebrew/test/cask/cmd/outdated_spec.rb b/Library/Homebrew/test/cask/cmd/outdated_spec.rb index 6cdf88014e..a850f6eb56 100644 --- a/Library/Homebrew/test/cask/cmd/outdated_spec.rb +++ b/Library/Homebrew/test/cask/cmd/outdated_spec.rb @@ -88,4 +88,104 @@ describe Cask::Cmd::Outdated, :cask do EOS end end + + describe "--json" do + it "lists outdated Casks in JSON format" do + result = [ + { + name: "local-caffeine", + installed_versions: "1.2.2", + current_version: "1.2.3" + }, + { + name: "local-transmission", + installed_versions: "2.60", + current_version: "2.61" + } + ].to_json + + expect { + described_class.run("--json") + }.to output(result + "\n").to_stdout + end + end + + describe "--json overrides --quiet" do + it "ignores --quiet and lists outdated Casks in JSON format" do + result = [ + { + name: "local-caffeine", + installed_versions: "1.2.2", + current_version: "1.2.3" + }, + { + name: "local-transmission", + installed_versions: "2.60", + current_version: "2.61" + } + ].to_json + + expect { + described_class.run("--json", "--quiet") + }.to output(result + "\n").to_stdout + end + end + + describe "--json and --greedy" do + it 'includes the Casks with "auto_updates true" or "version latest" in JSON format' do + result = [ + { + name: "auto-updates", + installed_versions: "2.57", + current_version: "2.61" + }, + { + name: "local-caffeine", + installed_versions: "1.2.2", + current_version: "1.2.3" + }, + { + name: "local-transmission", + installed_versions: "2.60", + current_version: "2.61" + }, + { + name: "version-latest-string", + installed_versions: "latest", + current_version: "latest" + } + ].to_json + + expect { + described_class.run("--json", "--greedy") + }.to output(result + "\n").to_stdout + end + + it 'does not include the Casks with "auto_updates true" with no version change in JSON format' do + cask = Cask::CaskLoader.load(cask_path("auto-updates")) + InstallHelper.install_with_caskfile(cask) + + result = [ + { + name: "local-caffeine", + installed_versions: "1.2.2", + current_version: "1.2.3" + }, + { + name: "local-transmission", + installed_versions: "2.60", + current_version: "2.61" + }, + { + name: "version-latest-string", + installed_versions: "latest", + current_version: "latest" + } + ].to_json + + expect { + described_class.run("--json", "--greedy") + }.to output(result + "\n").to_stdout + end + end end