From cd448ec16dd88e2d3ea1c55ce42f488639bbf717 Mon Sep 17 00:00:00 2001 From: Kit Plummer Date: Sun, 13 Nov 2022 12:25:16 -0500 Subject: [PATCH 1/3] update: repair warnings on specs, updated scan tests --- lib/git_helper.ex | 6 ++---- lib/git_module.ex | 19 +++++++++++++++---- lib/hex/encoder.ex | 1 - lib/hex/hex_scanner.ex | 6 ++---- lib/hex/lockfile.ex | 2 +- lib/hex/mixfile.ex | 2 +- lib/time_helper.ex | 2 -- test/fixtures/package-lockjson | 24 ------------------------ test/fixtures/packagejson | 7 +------ test/fixtures/yarnlock | 18 ------------------ test/mix_scan_test.exs | 14 +++++++------- 11 files changed, 29 insertions(+), 72 deletions(-) diff --git a/lib/git_helper.ex b/lib/git_helper.ex index 27d4c65..2f9227b 100644 --- a/lib/git_helper.ex +++ b/lib/git_helper.ex @@ -71,7 +71,8 @@ defmodule GitHelper do get_contributor_counts/1: Gets the number of contributions belonging to each author and return a map of %{name => number} """ def get_contributor_counts(list) do - get_contributor_counts(list, %{}) + counts = get_contributor_counts(list, %{}) + counts end @doc """ @@ -186,7 +187,6 @@ defmodule GitHelper do {:ok, accumulator} end - @spec get_contributor_counts([any], contrib_count) :: {:ok, [contrib_count], non_neg_integer} defp get_contributor_counts([head | tail], accumulator) do if head == "" do get_contributor_counts(tail, accumulator) @@ -206,7 +206,6 @@ defmodule GitHelper do end end - @spec get_contributor_counts([], non_neg_integer) :: {:ok, non_neg_integer} defp get_contributor_counts([], accumulator) do {:ok, accumulator} end @@ -216,7 +215,6 @@ defmodule GitHelper do 10 * length(String.split(x, " ")) + String.length(x) end - @spec filter_contributors([any]) :: [any] defp filter_contributors([]) do [] end diff --git a/lib/git_module.ex b/lib/git_module.ex index f2a328b..e813698 100644 --- a/lib/git_module.ex +++ b/lib/git_module.ex @@ -64,7 +64,18 @@ defmodule GitModule do {:ok, date} end - @spec delete_repo(Git.Repository.t()) :: String.t() + @spec delete_repo( + atom + | %{ + :path => + binary + | maybe_improper_list( + binary | maybe_improper_list(any, binary | []) | char, + binary | [] + ), + optional(any) => any + } + ) :: [binary] def delete_repo(repo) do File.rm_rf!(repo.path) end @@ -102,11 +113,11 @@ defmodule GitModule do {:ok, dates_int} end + @spec get_tag_and_commit_dates(Git.Repository.t()) :: {:ok, [[...]]} @doc """ get_tag_and_commit_dates/1: returns a list of lists of unix timestamps representing commit times with each lsit belonging to a different tag """ - @spec get_tag_and_commit_dates(Git.Repository.t()) :: [any] def get_tag_and_commit_dates(repo) do tag_and_date = git_log_split(repo, ["--pretty=format:%d$%ct"]) @@ -159,10 +170,10 @@ defmodule GitModule do {:ok, String.to_integer(line_num), String.to_integer(file_num)} end + @spec get_recent_changes(Git.Repository.t()) :: {:ok, number, number} @doc """ get_recent_changes/1: returns the percentage of changed lines in the last commit by the total lines in the repo """ - @spec get_recent_changes(Git.Repository.t()) :: {:ok, float} def get_recent_changes(repo) do with {:ok, total_lines, total_files_changed} <- get_total_lines(repo), {:ok, file_num, insertions, deletions} = get_last_2_delta(repo) do @@ -255,7 +266,7 @@ defmodule GitModule do {:ok, map} end - @spec get_clean_contributions_map(Git.Repository.t()) :: {:ok, map} + @spec get_clean_contributions_map(Git.Repository.t()) :: {:ok, list} def get_clean_contributions_map(repo) do map = Git.shortlog!(repo, ["-n", "-e", "HEAD", "--"]) diff --git a/lib/hex/encoder.ex b/lib/hex/encoder.ex index 50b368d..78f2d34 100644 --- a/lib/hex/encoder.ex +++ b/lib/hex/encoder.ex @@ -59,7 +59,6 @@ defmodule Hex.Encoder do Map.get(item, :tag) || Map.get(item, :branch) || "HEAD" end - @spec lockfile_json(map) :: charlist def lockfile_json(dependencies_full) do dependencies_full |> instruct() diff --git a/lib/hex/hex_scanner.ex b/lib/hex/hex_scanner.ex index 65f16b8..d1786a4 100644 --- a/lib/hex/hex_scanner.ex +++ b/lib/hex/hex_scanner.ex @@ -9,7 +9,6 @@ defmodule Hex.Scanner do Scanner scans for mix dependencies to run analysis on. """ - @spec scan(boolean(), map) :: {[], 0} def scan(mix?, _project_types) when mix? == false, do: {[], 0} @doc """ @@ -18,11 +17,11 @@ defmodule Hex.Scanner do """ @spec scan(boolean(), %{node: []}) :: {[any], non_neg_integer} def scan(_mix?, %{mix: [path_to_mix_exs | path_to_mix_lock]}) do - {_mixfile, deps_count} = + {:ok, {_mixfile, deps_count}} = File.read!(path_to_mix_exs) |> Hex.Mixfile.parse!() - {lockfile, _count} = + {:ok, {lockfile, _count}} = File.read!(path_to_mix_lock) |> Hex.Lockfile.parse!() @@ -37,7 +36,6 @@ defmodule Hex.Scanner do {result_map, deps_count} end - @spec query_hex(String.t()) :: {:ok, map} | String.t() defp query_hex(package) do HTTPoison.start() diff --git a/lib/hex/lockfile.ex b/lib/hex/lockfile.ex index 8a2785d..fd86d7f 100644 --- a/lib/hex/lockfile.ex +++ b/lib/hex/lockfile.ex @@ -36,7 +36,7 @@ defmodule Hex.Lockfile do |> Code.string_to_quoted(file: "mix.lock", warn_on_unnecessary_quotes: false) |> extract_deps() - {deps, length(deps)} + {:ok, {deps, length(deps)}} end def parse!(content, _do_no_extract) do diff --git a/lib/hex/mixfile.ex b/lib/hex/mixfile.ex index 8eb596c..ea25067 100644 --- a/lib/hex/mixfile.ex +++ b/lib/hex/mixfile.ex @@ -39,7 +39,7 @@ defmodule Hex.Mixfile do |> extract_deps() |> Enum.to_list() - {deps, length(deps)} + {:ok, {deps, length(deps)}} end @impl Parser diff --git a/lib/time_helper.ex b/lib/time_helper.ex index 8e75b32..244d09d 100644 --- a/lib/time_helper.ex +++ b/lib/time_helper.ex @@ -55,7 +55,6 @@ defmodule TimeHelper do @doc """ get_commit_delta/1: returns the time between now and the last commit in seconds """ - @spec get_commit_delta(String.t()) :: {:ok, String.t()} | {:error, String.t()} def get_commit_delta(last_commit_date) do case DateTime.from_iso8601(last_commit_date) do {:error, error} -> @@ -70,7 +69,6 @@ defmodule TimeHelper do @doc """ sum_ts_diff/2 """ - @spec sum_ts_diff([any], non_neg_integer) :: {:ok, non_neg_integer} def sum_ts_diff([_head | []], accumulator) do {:ok, accumulator} end diff --git a/test/fixtures/package-lockjson b/test/fixtures/package-lockjson index c5681d8..6fdc7e5 100644 --- a/test/fixtures/package-lockjson +++ b/test/fixtures/package-lockjson @@ -4,30 +4,6 @@ "lockfileVersion": 1, "requires": true, "dependencies": { - "ajv": { - "version": "6.10.2", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.10.2.tgz", - "integrity": "sha512-TXtUUEYHuaTEbLZWIKUr5pmBuhDLy+8KYtPYdcV8qC+pOZL+NKqYwvWSRrVXHn+ZmRRAu8vJTAznH7Oag6RVRw==", - "requires": { - "fast-deep-equal": "^2.0.1", - "fast-json-stable-stringify": "^2.0.0", - "json-schema-traverse": "^0.4.1", - "uri-js": "^4.2.2" - } - }, - "assert-plus": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz", - "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=" - }, - "bcrypt-pbkdf": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", - "integrity": "sha1-pDAdOJtqQ/m2f/PKEaP2Y342Dp4=", - "requires": { - "tweetnacl": "^0.14.3" - } - }, "combined-stream": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", diff --git a/test/fixtures/packagejson b/test/fixtures/packagejson index d5530ed..04af95a 100644 --- a/test/fixtures/packagejson +++ b/test/fixtures/packagejson @@ -9,13 +9,8 @@ "keywords": [], "author": "", "license": "ISC", - "dependencies": { - "request": "^2.88.0" - }, "devDependencies": { - "async": "^2.1.4", - "benchmark": "^2.1.3", - "chalk": "^1.1.3" + "simple-npm-package": "^3.0.8" } } \ No newline at end of file diff --git a/test/fixtures/yarnlock b/test/fixtures/yarnlock index 2b6bf99..517b971 100644 --- a/test/fixtures/yarnlock +++ b/test/fixtures/yarnlock @@ -1,24 +1,6 @@ # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. # yarn lockfile v1 - -ajv@^6.12.3: - version "6.12.3" - resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.12.3.tgz#18c5af38a111ddeb4f2697bd78d68abc1cabd706" - integrity sha512-4K0cK3L1hsqk9xIb2z9vs/XU+PGJZ9PNpJRDS9YLzmNdX6jmVPfamLvTJr0aDAusnHyCHO6MjzlkAsgtqp9teA== - dependencies: - fast-deep-equal "^3.1.1" - fast-json-stable-stringify "^2.0.0" - json-schema-traverse "^0.4.1" - uri-js "^4.2.2" - -asn1@~0.2.3: - version "0.2.4" - resolved "https://registry.yarnpkg.com/asn1/-/asn1-0.2.4.tgz#8d2475dfab553bb33e77b54e59e880bb8ce23136" - integrity sha512-jxwzQpLQjSmWXgwaCZE9Nz+glAG01yF1QnWgbhGwHI5A6FRIEY6IVqtHhIepHqI7/kyEyQEagBC5mBEFlIYvdg== - dependencies: - safer-buffer "~2.1.0" - assert-plus@1.0.0, assert-plus@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/assert-plus/-/assert-plus-1.0.0.tgz#f12e0f3c5d77b0b1cdd9146942e4e96c1e4dd525" diff --git a/test/mix_scan_test.exs b/test/mix_scan_test.exs index 9f17db5..e906663 100644 --- a/test/mix_scan_test.exs +++ b/test/mix_scan_test.exs @@ -75,16 +75,16 @@ defmodule Mix.Tasks.ScanTest do path = %{node: ["./test/fixtures/packagejson"]} {reports_list, [], deps_count} = Npm.Scanner.scan(true, path, "") - assert 4 == deps_count - assert 4 == Enum.count(reports_list) + assert 1 == deps_count + assert 2 == Enum.count(reports_list) end test "run scan against package.json and yarn.lock" do paths = %{node: ["./test/fixtures/packagejson", "./test/fixtures/yarnlock"]} {[], reports_list, deps_count} = Npm.Scanner.scan(true, paths, "") - assert 4 == deps_count - assert 3 == Enum.count(reports_list) + assert 2 == deps_count + assert 1 == Enum.count(reports_list) end @moduletag timeout: 200000 @@ -99,9 +99,9 @@ defmodule Mix.Tasks.ScanTest do {json_reports_list, yarn_reports_list, deps_count} = Npm.Scanner.scan(true, paths, "") - assert 4 == deps_count - assert 4 == Enum.count(json_reports_list) - assert 3 == Enum.count(yarn_reports_list) + assert 1 == deps_count + assert 1 == Enum.count(json_reports_list) + assert 1 == Enum.count(yarn_reports_list) end @tag timeout: 140_000 From 5f857c937da7a3d0c232927cea2cdc9ec6a4bd44 Mon Sep 17 00:00:00 2001 From: Kit Plummer Date: Sun, 13 Nov 2022 13:18:15 -0500 Subject: [PATCH 2/3] fix: hex tests --- test/hex/encoder_test.exs | 4 ++-- test/hex/lockfile_test.exs | 2 +- test/hex/mixfile_test.exs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/hex/encoder_test.exs b/test/hex/encoder_test.exs index 32264ec..792b4ab 100644 --- a/test/hex/encoder_test.exs +++ b/test/hex/encoder_test.exs @@ -6,7 +6,7 @@ defmodule Lowendinsight.Hex.EncoderTest do use ExUnit.Case, async: true test "encoder works for mix.exs" do - {deps, count} = + {:ok, {deps, count}} = File.read!("./test/fixtures/mixfile") |> Hex.Mixfile.parse!() @@ -21,7 +21,7 @@ defmodule Lowendinsight.Hex.EncoderTest do end test "encoder works for mix.lock" do - {deps, count} = + {:ok, {deps, count}} = File.read!("./test/fixtures/lockfile") |> Hex.Lockfile.parse!() diff --git a/test/hex/lockfile_test.exs b/test/hex/lockfile_test.exs index b6c040f..11300be 100644 --- a/test/hex/lockfile_test.exs +++ b/test/hex/lockfile_test.exs @@ -6,7 +6,7 @@ defmodule LockfileTest do use ExUnit.Case test "extracts dependencies from mix.lock" do - {lib_map, deps_count} = Hex.Lockfile.parse!(File.read!("./test/fixtures/lockfile")) + {:ok, {lib_map, deps_count}} = Hex.Lockfile.parse!(File.read!("./test/fixtures/lockfile")) parsed_lockfile = [ {:hex, :cowboy, "1.0.4"}, diff --git a/test/hex/mixfile_test.exs b/test/hex/mixfile_test.exs index d1b8247..6e49494 100644 --- a/test/hex/mixfile_test.exs +++ b/test/hex/mixfile_test.exs @@ -6,7 +6,7 @@ defmodule MixfileTest do use ExUnit.Case test "extracts dependencies from mix.exs" do - {lib_map, deps_count} = Hex.Mixfile.parse!(File.read!("./test/fixtures/mixfile")) + {:ok, {lib_map, deps_count}} = Hex.Mixfile.parse!(File.read!("./test/fixtures/mixfile")) parsed_mixfile = [oauth: [github: "tim/erlang-oauth"], poison: "~> 1.3.1", plug: "~> 0.11.0"] assert deps_count == 3 From 37ca8fdc02d484623c85d8c1813ce335efff6b85 Mon Sep 17 00:00:00 2001 From: Kit Plummer Date: Sun, 13 Nov 2022 13:35:17 -0500 Subject: [PATCH 3/3] fix: more test fixes --- test/mix_scan_test.exs | 8 ++++---- test/npm/package_json_test.exs | 12 +++--------- test/npm/yarnlock_test.exs | 4 ++-- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/test/mix_scan_test.exs b/test/mix_scan_test.exs index e906663..d2e48d9 100644 --- a/test/mix_scan_test.exs +++ b/test/mix_scan_test.exs @@ -67,8 +67,8 @@ defmodule Mix.Tasks.ScanTest do paths = %{node: ["./test/fixtures/packagejson", "./test/fixtures/package-lockjson"]} {reports_list, [], deps_count} = Npm.Scanner.scan(true, paths, "") - assert 4 == deps_count - assert 4 == Enum.count(reports_list) + assert 1 == deps_count + assert 1 == Enum.count(reports_list) end test "run scan against first-degree dependencies if package-lock does not exist" do @@ -76,14 +76,14 @@ defmodule Mix.Tasks.ScanTest do {reports_list, [], deps_count} = Npm.Scanner.scan(true, path, "") assert 1 == deps_count - assert 2 == Enum.count(reports_list) + assert 1 == Enum.count(reports_list) end test "run scan against package.json and yarn.lock" do paths = %{node: ["./test/fixtures/packagejson", "./test/fixtures/yarnlock"]} {[], reports_list, deps_count} = Npm.Scanner.scan(true, paths, "") - assert 2 == deps_count + assert 1 == deps_count assert 1 == Enum.count(reports_list) end diff --git a/test/npm/package_json_test.exs b/test/npm/package_json_test.exs index 3d61560..3d34bea 100644 --- a/test/npm/package_json_test.exs +++ b/test/npm/package_json_test.exs @@ -9,13 +9,10 @@ defmodule PackageJSONTest do {lib_map, deps_count} = Npm.Packagefile.parse!(File.read!("./test/fixtures/packagejson")) parsed_package_json = [ - {"async", "2.1.4"}, - {"benchmark", "2.1.3"}, - {"chalk", "1.1.3"}, - {"request", "2.88.0"} + {"simple-npm-package", "3.0.8"} ] - assert deps_count == 4 + assert deps_count == 1 assert parsed_package_json == lib_map end @@ -23,13 +20,10 @@ defmodule PackageJSONTest do {lib_map, deps_count} = Npm.Packagefile.parse!(File.read!("./test/fixtures/package-lockjson")) parsed_package_lock_json = [ - {"ajv", "6.10.2"}, - {"assert-plus", "1.0.0"}, - {"bcrypt-pbkdf", "1.0.2"}, {"combined-stream", "1.0.8"} ] - assert deps_count == 4 + assert deps_count == 1 assert parsed_package_lock_json == lib_map end end diff --git a/test/npm/yarnlock_test.exs b/test/npm/yarnlock_test.exs index a16091d..14a8732 100644 --- a/test/npm/yarnlock_test.exs +++ b/test/npm/yarnlock_test.exs @@ -8,9 +8,9 @@ defmodule YarnlockTest do test "extracts dependencies from yarn.lock" do {lib_map, deps_count} = Npm.Yarnlockfile.parse!(File.read!("./test/fixtures/yarnlock")) - parsed_yarn = [{"ajv", "6.12.3"}, {"asn1", "0.2.4"}, {"assert-plus", "1.0.0"}] + parsed_yarn = [{"assert-plus", "1.0.0"}] - assert deps_count == 3 + assert deps_count == 1 assert parsed_yarn == lib_map end end