Skip to content

Commit

Permalink
Merge pull request #120 from gtri/warnings_fixes
Browse files Browse the repository at this point in the history
update: repair warnings on specs, updated scan tests
  • Loading branch information
kitplummer authored Nov 13, 2022
2 parents c5c44dd + 37ca8fd commit da4ce6a
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 89 deletions.
6 changes: 2 additions & 4 deletions lib/git_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
19 changes: 15 additions & 4 deletions lib/git_module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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", "--"])
Expand Down
1 change: 0 additions & 1 deletion lib/hex/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 2 additions & 4 deletions lib/hex/hex_scanner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand All @@ -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!()

Expand All @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion lib/hex/lockfile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/hex/mixfile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule Hex.Mixfile do
|> extract_deps()
|> Enum.to_list()

{deps, length(deps)}
{:ok, {deps, length(deps)}}
end

@impl Parser
Expand Down
2 changes: 0 additions & 2 deletions lib/time_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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} ->
Expand All @@ -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
Expand Down
24 changes: 0 additions & 24 deletions test/fixtures/package-lockjson
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 1 addition & 6 deletions test/fixtures/packagejson
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

18 changes: 0 additions & 18 deletions test/fixtures/yarnlock
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 2 additions & 2 deletions test/hex/encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()

Expand All @@ -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!()

Expand Down
2 changes: 1 addition & 1 deletion test/hex/lockfile_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
2 changes: 1 addition & 1 deletion test/hex/mixfile_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions test/mix_scan_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,24 @@ 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
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 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 4 == deps_count
assert 3 == Enum.count(reports_list)
assert 1 == deps_count
assert 1 == Enum.count(reports_list)
end

@moduletag timeout: 200000
Expand All @@ -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
Expand Down
12 changes: 3 additions & 9 deletions test/npm/package_json_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,21 @@ 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

test "extracts dependencies from package-lock.json" 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
4 changes: 2 additions & 2 deletions test/npm/yarnlock_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit da4ce6a

Please sign in to comment.