Skip to content

Commit

Permalink
Remove core and re2 as dependency (#13)
Browse files Browse the repository at this point in the history
* Make core a test-only dependency

* Remove core as dependency in tests as well

And further reduce the dependency cone.

* Switch from re2 to re

The dependency cone for re is much smaller than for re2.

* CI: install also test dependencies

* Fix regexp

Re.Str.regexp_string does not do what I expected. Oops!

* Remove reference to core in test/

* Remove Core from test/malformed.ml
  • Loading branch information
reynir authored Jan 23, 2024
1 parent db82958 commit 3aa2fcf
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 121 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
with:
ocaml-compiler: ${{ matrix.ocaml-compiler }}

- run: opam install . --deps-only
- run: opam install . --deps-only --with-test

- run: opam exec -- dune build

Expand Down
4 changes: 2 additions & 2 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
(name sarif)
(synopsis "Static Analysis Results Interchange Format (SARIF) Version 2.1.0")
(description "Static Analysis Results Interchange Format (SARIF) Version 2.1.0")
(depends (ocaml (>= 4.14.0)) dune core re2 (atdgen (>= 2.10.0)) (atdgen-runtime (>= 2.10.0)) timedesc ppx_jane ppx_deriving (uri (>= 4.4.0)))
(depends (ocaml (>= 4.14.0)) dune re (atdgen (>= 2.10.0)) (atdgen-runtime (>= 2.10.0)) timedesc (ppx_expect :with-test) ppx_deriving (uri (>= 4.4.0)))
(tags
(sarif)))
(sarif)))
2 changes: 1 addition & 1 deletion lib/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(library
(public_name sarif)
(name sarif)
(libraries core timedesc atdgen-runtime re2 uri)
(libraries timedesc atdgen-runtime re uri)
(preprocess (pps ppx_deriving.show ppx_deriving.ord ppx_deriving.eq))
(flags :standard -w -30))

Expand Down
67 changes: 37 additions & 30 deletions lib/sarif_v_2_1_0_util.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(** Validation functions used by atdgen validator *)

open Core
open Timedesc

open Sarif_v_2_1_0_t
Expand All @@ -11,12 +10,14 @@ let validate_iso8601_opt = function
| Ok _ -> true
| Error _ -> false

let re_mime_type =
Re.Str.regexp "^[^/]+/.+$"

let validate_mime_type x =
let re = Re2.create_exn "[^/]+/.+" in Re2.matches re x
Re.Str.string_match re_mime_type x 0

let validate_mime_type_opt = function
| None -> true
| Some v -> let re = Re2.create_exn "[^/]+/.+" in Re2.matches re v
let validate_mime_type_opt x =
Option.fold ~none:true ~some:validate_mime_type x

let validate_int64_minimum_zero x = if (Int64.compare x (-1L)) > 0 then true else false

Expand All @@ -33,40 +34,46 @@ let validate_int64_minimum_one_opt = function
let validate_int64_minimum_minus_one x =
if (Int64.compare x (-2L)) > 0 then true else false

let re_guid =
Re.Str.regexp "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"

let validate_guid x =
let re = Re2.create_exn "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"
in Re2.matches re x
Re.Str.string_match re_guid x 0

let validate_guid_opt = function
| None -> true
| Some v -> let re = Re2.create_exn "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"
in Re2.matches re v
let validate_guid_opt o =
Option.fold o ~none:true ~some:validate_guid

let re_dotted_quad_file =
Re.Str.regexp "^[0-9]+(\\\\.[0-9]+){3}$"

let validate_dotted_quad_file_v x =
let re = Re2.create_exn "[0-9]+(\\\\.[0-9]+){3}"
in Re2.matches re x
Re.Str.string_match re_dotted_quad_file x 0

let validate_dotted_quad_file_v_opt = function
| None -> true
| Some v -> let re = Re2.create_exn "[0-9]+(\\\\.[0-9]+){3}" in Re2.matches re v
let validate_dotted_quad_file_v_opt o =
Option.fold o ~none:true ~some:validate_dotted_quad_file_v

let re_language =
Re.Str.regexp "^[a-zA-Z]{2}(-[a-zA-Z]{2})?$"

let validate_language x =
let re = Re2.create_exn "^[a-zA-Z]{2}(-[a-zA-Z]{2})?$"
in Re2.matches re x
Re.Str.string_match re_language x 0

let validate_language_opt = function
| None -> true
| Some v -> let re = Re2.create_exn "^[a-zA-Z]{2}(-[a-zA-Z]{2})?$" in Re2.matches re v
let validate_language_opt x =
Option.fold x ~none:true ~some:validate_language

let validate_unique = function
| [] -> true
| lst -> if List.contains_dup lst ~compare:Stdlib.compare then false else true

let validate_unique_opt = function
| None -> true
| Some v -> match v with
| [] -> true
| lst -> if List.contains_dup lst ~compare:Stdlib.compare then false else true
| cur :: rem ->
let rec loop cur rem =
not (List.mem cur rem) &&
match rem with
| [] -> true
| cur :: rem -> loop cur rem
in
loop cur rem

let validate_unique_opt xs_opt =
Option.fold ~none:true ~some:validate_unique xs_opt

let validate_rank x =
if (Int64.compare x (-2L)) > 0 && (Int64.compare x (101L)) < 0 then true else false
Expand All @@ -87,12 +94,12 @@ let validate_list_min_size_one x = if (Int.compare (List.length x) 1) > 0 then t
let validate_list_all_str_list (lst : string list option) pred =
match lst with
| None -> true
| Some v -> List.for_all v ~f:pred
| Some v -> List.for_all pred v

let validate_list_all_deprecated_guid_list (lst : reporting_descriptor_deprecated_guids_item list option) pred =
match lst with
| None -> true
| Some v -> List.for_all v ~f:pred
| Some v -> List.for_all pred v

(** Validator for type address *)
let validate_address (address : address) =
Expand Down
5 changes: 2 additions & 3 deletions sarif.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ bug-reports: "https://github.com/gborough/sarif/issues"
depends: [
"ocaml" {>= "4.14.0"}
"dune" {>= "3.12"}
"core"
"re2"
"re"
"atdgen" {>= "2.10.0"}
"atdgen-runtime" {>= "2.10.0"}
"timedesc"
"ppx_jane"
"ppx_expect" {with-test}
"ppx_deriving"
"uri" {>= "4.4.0"}
"odoc" {with-doc}
Expand Down
6 changes: 3 additions & 3 deletions test/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(library
(name test_sarif)
(libraries sarif core ppx_expect.common ppx_expect.config ppx_expect.config_types)
(libraries sarif ppx_expect.common ppx_expect.config ppx_expect.config_types)
(inline_tests (deps (glob_files data/*)))
(preprocess (pps ppx_jane)))
(preprocess (pps ppx_expect)))

(include_subdirs unqualified)
(include_subdirs unqualified)
77 changes: 41 additions & 36 deletions test/malformed.ml
Original file line number Diff line number Diff line change
@@ -1,130 +1,135 @@
open Core
open Sarif

let read_all file =
let ic = open_in_bin file in
let s = really_input_string ic (in_channel_length ic) in
close_in ic;
s

let%expect_test "malformed_iso8601_date" =
let json = In_channel.read_all "data/malformed.json" in
let json = read_all "data/malformed.json" in
let parsed_all = Sarif_v_2_1_0_j.sarif_json_schema_of_string json in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd_exn @@ parsed_all.runs in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd @@ parsed_all.runs in
let parsed_run = Sarif_v_2_1_0_j.run_of_string run in
let artifact = Sarif_v_2_1_0_j.string_of_artifact @@ List.nth_exn (Option.value_exn parsed_run.artifacts) 0 in
let artifact = Sarif_v_2_1_0_j.string_of_artifact @@ List.nth (Option.get parsed_run.artifacts) 0 in
let parsed_artifact = Sarif_v_2_1_0_j.artifact_of_string artifact in
let res = Sarif_v_2_1_0_util.validate_artifact parsed_artifact in
if res then print_endline "true" else print_endline "false";
[%expect {|false|}]

let%expect_test "malformed_mime_type" =
let json = In_channel.read_all "data/malformed.json" in
let json = read_all "data/malformed.json" in
let parsed_all = Sarif_v_2_1_0_j.sarif_json_schema_of_string json in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd_exn @@ parsed_all.runs in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd @@ parsed_all.runs in
let parsed_run = Sarif_v_2_1_0_j.run_of_string run in
let artifact = Sarif_v_2_1_0_j.string_of_artifact @@ List.nth_exn (Option.value_exn parsed_run.artifacts) 1 in
let artifact = Sarif_v_2_1_0_j.string_of_artifact @@ List.nth (Option.get parsed_run.artifacts) 1 in
let parsed_artifact = Sarif_v_2_1_0_j.artifact_of_string artifact in
let res = Sarif_v_2_1_0_util.validate_artifact parsed_artifact in
if res then print_endline "true" else print_endline "false";
[%expect {|false|}]

let%expect_test "malformed_minimum_zero" =
let json = In_channel.read_all "data/malformed.json" in
let json = read_all "data/malformed.json" in
let parsed_all = Sarif_v_2_1_0_j.sarif_json_schema_of_string json in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd_exn @@ parsed_all.runs in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd @@ parsed_all.runs in
let parsed_run = Sarif_v_2_1_0_j.run_of_string run in
let artifact = Sarif_v_2_1_0_j.string_of_artifact @@ List.nth_exn (Option.value_exn parsed_run.artifacts) 2 in
let artifact = Sarif_v_2_1_0_j.string_of_artifact @@ List.nth (Option.get parsed_run.artifacts) 2 in
let parsed_artifact = Sarif_v_2_1_0_j.artifact_of_string artifact in
let res = Sarif_v_2_1_0_util.validate_artifact parsed_artifact in
if res then print_endline "true" else print_endline "false";
[%expect {|false|}]

let%expect_test "malformed_minimum_one" =
let json = In_channel.read_all "data/malformed.json" in
let json = read_all "data/malformed.json" in
let parsed_all = Sarif_v_2_1_0_j.sarif_json_schema_of_string json in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd_exn @@ parsed_all.runs in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd @@ parsed_all.runs in
let parsed_run = Sarif_v_2_1_0_j.run_of_string run in
let result = Sarif_v_2_1_0_j.string_of_result @@ List.nth_exn (Option.value_exn parsed_run.results) 0 in
let result = Sarif_v_2_1_0_j.string_of_result @@ List.nth (Option.get parsed_run.results) 0 in
let parsed_result = Sarif_v_2_1_0_j.result_of_string result in
let res = Sarif_v_2_1_0_util.validate_result parsed_result in
if res then print_endline "true" else print_endline "false";
[%expect {|false|}]

let%expect_test "malformed_minimum_minus_one" =
let json = In_channel.read_all "data/malformed.json" in
let json = read_all "data/malformed.json" in
let parsed_all = Sarif_v_2_1_0_j.sarif_json_schema_of_string json in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd_exn @@ parsed_all.runs in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd @@ parsed_all.runs in
let parsed_run = Sarif_v_2_1_0_j.run_of_string run in
let artifact = Sarif_v_2_1_0_j.string_of_artifact @@ List.nth_exn (Option.value_exn parsed_run.artifacts) 3 in
let artifact = Sarif_v_2_1_0_j.string_of_artifact @@ List.nth (Option.get parsed_run.artifacts) 3 in
let parsed_artifact = Sarif_v_2_1_0_j.artifact_of_string artifact in
let res = Sarif_v_2_1_0_util.validate_artifact parsed_artifact in
if res then print_endline "true" else print_endline "false";
[%expect {|false|}]

let%expect_test "malformed_guid" =
let json = In_channel.read_all "data/malformed.json" in
let json = read_all "data/malformed.json" in
let parsed_all = Sarif_v_2_1_0_j.sarif_json_schema_of_string json in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd_exn @@ parsed_all.runs in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd @@ parsed_all.runs in
let parsed_run = Sarif_v_2_1_0_j.run_of_string run in
let result = Sarif_v_2_1_0_j.string_of_result @@ List.nth_exn (Option.value_exn parsed_run.results) 1 in
let result = Sarif_v_2_1_0_j.string_of_result @@ List.nth (Option.get parsed_run.results) 1 in
let parsed_result = Sarif_v_2_1_0_j.result_of_string result in
let res = Sarif_v_2_1_0_util.validate_result parsed_result in
if res then print_endline "true" else print_endline "false";
[%expect {|false|}]

let%expect_test "malformed_dotted_quad_file" =
let json = In_channel.read_all "data/malformed.json" in
let json = read_all "data/malformed.json" in
let parsed_all = Sarif_v_2_1_0_j.sarif_json_schema_of_string json in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd_exn @@ parsed_all.runs in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd @@ parsed_all.runs in
let parsed_run = Sarif_v_2_1_0_j.run_of_string run in
let taxonomies = Sarif_v_2_1_0_j.string_of_tool_component @@ List.hd_exn @@ Option.value_exn parsed_run.taxonomies in
let taxonomies = Sarif_v_2_1_0_j.string_of_tool_component @@ List.hd @@ Option.get parsed_run.taxonomies in
let parsed_taxonomies = Sarif_v_2_1_0_j.tool_component_of_string taxonomies in
let res = Sarif_v_2_1_0_util.validate_tool_component parsed_taxonomies in
if res then print_endline "true" else print_endline "false";
[%expect {|false|}]

let%expect_test "malformed_language" =
let json = In_channel.read_all "data/malformed1.json" in
let json = read_all "data/malformed1.json" in
let parsed_all = Sarif_v_2_1_0_j.sarif_json_schema_of_string json in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd_exn @@ parsed_all.runs in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd @@ parsed_all.runs in
let parsed_run = Sarif_v_2_1_0_j.run_of_string run in
let res = Sarif_v_2_1_0_util.validate_run parsed_run in
if res then print_endline "true" else print_endline "false";
[%expect {|false|}]

let%expect_test "malformed_none_unique" =
let json = In_channel.read_all "data/malformed1.json" in
let json = read_all "data/malformed1.json" in
let parsed_all = Sarif_v_2_1_0_j.sarif_json_schema_of_string json in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd_exn @@ parsed_all.runs in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd @@ parsed_all.runs in
let parsed_run = Sarif_v_2_1_0_j.run_of_string run in
let result = Sarif_v_2_1_0_j.string_of_result @@ List.hd_exn @@ Option.value_exn parsed_run.results in
let result = Sarif_v_2_1_0_j.string_of_result @@ List.hd @@ Option.get parsed_run.results in
let parsed_result = Sarif_v_2_1_0_j.result_of_string result in
let res = Sarif_v_2_1_0_util.validate_result parsed_result in
if res then print_endline "true" else print_endline "false";
[%expect {|false|}]

let%expect_test "malformed_rank" =
let json = In_channel.read_all "data/malformed2.json" in
let json = read_all "data/malformed2.json" in
let parsed_all = Sarif_v_2_1_0_j.sarif_json_schema_of_string json in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd_exn @@ parsed_all.runs in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd @@ parsed_all.runs in
let parsed_run = Sarif_v_2_1_0_j.run_of_string run in
let result = Sarif_v_2_1_0_j.string_of_result @@ List.hd_exn @@ Option.value_exn parsed_run.results in
let result = Sarif_v_2_1_0_j.string_of_result @@ List.hd @@ Option.get parsed_run.results in
let parsed_result = Sarif_v_2_1_0_j.result_of_string result in
let res = Sarif_v_2_1_0_util.validate_result parsed_result in
if res then print_endline "true" else print_endline "false";
[%expect {|false|}]

let%expect_test "malformed_uri" =
let json = In_channel.read_all "data/malformed3.json" in
let json = read_all "data/malformed3.json" in
let parsed_all = Sarif_v_2_1_0_j.sarif_json_schema_of_string json in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd_exn @@ parsed_all.runs in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd @@ parsed_all.runs in
let parsed_run = Sarif_v_2_1_0_j.run_of_string run in
let result = Sarif_v_2_1_0_j.string_of_result @@ List.hd_exn @@ Option.value_exn parsed_run.results in
let result = Sarif_v_2_1_0_j.string_of_result @@ List.hd @@ Option.get parsed_run.results in
let parsed_result = Sarif_v_2_1_0_j.result_of_string result in
let res = Sarif_v_2_1_0_util.validate_result parsed_result in
if res then print_endline "true" else print_endline "false";
[%expect {|false|}]

let%expect_test "malformed_list_min_size_one" =
let json = In_channel.read_all "data/malformed4.json" in
let json = read_all "data/malformed4.json" in
let parsed_all = Sarif_v_2_1_0_j.sarif_json_schema_of_string json in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd_exn @@ parsed_all.runs in
let run = Sarif_v_2_1_0_j.string_of_run @@ List.hd @@ parsed_all.runs in
let parsed_run = Sarif_v_2_1_0_j.run_of_string run in
let res = Sarif_v_2_1_0_util.validate_run parsed_run in
if res then print_endline "true" else print_endline "false";
[%expect {|false|}]
[%expect {|false|}]
Loading

0 comments on commit 3aa2fcf

Please sign in to comment.