From 753b8621dfe3630e9e78ed286b59980c8efc9641 Mon Sep 17 00:00:00 2001 From: Seeker Date: Tue, 4 Aug 2020 10:07:57 -0700 Subject: [PATCH 1/2] utils: add SPDX module --- Library/Homebrew/dev-cmd/audit.rb | 4 ++-- .../Homebrew/dev-cmd/update-license-data.rb | 13 ++++-------- Library/Homebrew/utils/spdx.rb | 20 +++++++++++++++++++ 3 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 Library/Homebrew/utils/spdx.rb diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 45b24c293d..ed990fdc45 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -4,6 +4,7 @@ require "formula" require "formula_versions" require "utils/curl" require "utils/notability" +require "utils/spdx" require "extend/ENV" require "formula_cellar_checks" require "cmd/search" @@ -118,8 +119,7 @@ module Homebrew # Check style in a single batch run up front for performance style_results = Style.check_style_json(style_files, options) if style_files # load licenses - spdx = HOMEBREW_LIBRARY_PATH/"data/spdx.json" - spdx_data = JSON.parse(spdx.read) + spdx_data = SPDX.spdx_data new_formula_problem_lines = [] audit_formulae.sort.each do |f| only = only_cops ? ["style"] : args.only diff --git a/Library/Homebrew/dev-cmd/update-license-data.rb b/Library/Homebrew/dev-cmd/update-license-data.rb index 40584b3b4b..7836f069a9 100644 --- a/Library/Homebrew/dev-cmd/update-license-data.rb +++ b/Library/Homebrew/dev-cmd/update-license-data.rb @@ -1,14 +1,11 @@ # frozen_string_literal: true require "cli/parser" -require "utils/github" +require "utils/spdx" module Homebrew module_function - SPDX_PATH = (HOMEBREW_LIBRARY_PATH/"data/spdx.json").freeze - SPDX_API_URL = "https://api.github.com/repos/spdx/license-list-data/releases/latest" - def update_license_data_args Homebrew::CLI::Parser.new do usage_banner <<~EOS @@ -29,16 +26,14 @@ module Homebrew args = update_license_data_args.parse ohai "Updating SPDX license data..." - latest_tag = GitHub.open_api(SPDX_API_URL)["tag_name"] - data_url = "https://raw.githubusercontent.com/spdx/license-list-data/#{latest_tag}/json/licenses.json" - curl_download(data_url, to: SPDX_PATH, partial: false) + SPDX.download_latest_license_data! - Homebrew.failed = system("git", "diff", "--stat", "--exit-code", SPDX_PATH) if args.fail_if_not_changed? + Homebrew.failed = system("git", "diff", "--stat", "--exit-code", SPDX::JSON_PATH) if args.fail_if_not_changed? return unless args.commit? ohai "git add" - safe_system "git", "add", SPDX_PATH + safe_system "git", "add", SPDX::JSON_PATH ohai "git commit" system "git", "commit", "--message", "data/spdx.json: update to #{latest_tag}" end diff --git a/Library/Homebrew/utils/spdx.rb b/Library/Homebrew/utils/spdx.rb new file mode 100644 index 0000000000..a2c5fd4f12 --- /dev/null +++ b/Library/Homebrew/utils/spdx.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require "utils/github" + +module SPDX + module_function + + JSON_PATH = (HOMEBREW_LIBRARY_PATH/"data/spdx.json").freeze + API_URL = "https://api.github.com/repos/spdx/license-list-data/releases/latest" + + def spdx_data + @spdx_data ||= JSON.parse(JSON_PATH.read) + end + + def download_latest_license_data! + latest_tag = GitHub.open_api(API_URL)["tag_name"] + data_url = "https://raw.githubusercontent.com/spdx/license-list-data/#{latest_tag}/json/licenses.json" + curl_download(data_url, to: JSON_PATH, partial: false) + end +end From 6ecef73131178459ab9aa787bd9eaaacb48a6300 Mon Sep 17 00:00:00 2001 From: Seeker Date: Thu, 6 Aug 2020 11:12:43 -0700 Subject: [PATCH 2/2] test: add spdx spec --- Library/Homebrew/test/utils/spdx_spec.rb | 32 ++++++++++++++++++++++++ Library/Homebrew/utils/spdx.rb | 4 +-- 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 Library/Homebrew/test/utils/spdx_spec.rb diff --git a/Library/Homebrew/test/utils/spdx_spec.rb b/Library/Homebrew/test/utils/spdx_spec.rb new file mode 100644 index 0000000000..e99cc5e610 --- /dev/null +++ b/Library/Homebrew/test/utils/spdx_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require "utils/spdx" + +describe SPDX do + describe ".spdx_data" do + it "has the license list version" do + expect(described_class.spdx_data["licenseListVersion"]).not_to eq(nil) + end + + it "has the release date" do + expect(described_class.spdx_data["releaseDate"]).not_to eq(nil) + end + + it "has licenses" do + expect(described_class.spdx_data["licenses"].length).not_to eq(0) + end + end + + describe ".download_latest_license_data!", :needs_network do + let(:tmp_json_path) { Pathname.new("#{TEST_TMPDIR}/spdx.json") } + + after do + FileUtils.rm tmp_json_path + end + + it "downloads latest license data" do + described_class.download_latest_license_data! to: tmp_json_path + expect(tmp_json_path).to exist + end + end +end diff --git a/Library/Homebrew/utils/spdx.rb b/Library/Homebrew/utils/spdx.rb index a2c5fd4f12..4ee6b59eb4 100644 --- a/Library/Homebrew/utils/spdx.rb +++ b/Library/Homebrew/utils/spdx.rb @@ -12,9 +12,9 @@ module SPDX @spdx_data ||= JSON.parse(JSON_PATH.read) end - def download_latest_license_data! + def download_latest_license_data!(to: JSON_PATH) latest_tag = GitHub.open_api(API_URL)["tag_name"] data_url = "https://raw.githubusercontent.com/spdx/license-list-data/#{latest_tag}/json/licenses.json" - curl_download(data_url, to: JSON_PATH, partial: false) + curl_download(data_url, to: to, partial: false) end end