File: versions.ml

package info (click to toggle)
opam-file-format 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 280 kB
  • sloc: ml: 2,356; makefile: 4
file content (107 lines) | stat: -rw-r--r-- 3,118 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
(**************************************************************************)
(*                                                                        *)
(*    Copyright 2021 David Allsopp Ltd.                                   *)
(*                                                                        *)
(*  All rights reserved. This file is distributed under the terms of the  *)
(*  GNU Lesser General Public License version 2.1, with the special       *)
(*  exception on linking described in the file LICENSE.                   *)
(*                                                                        *)
(**************************************************************************)

module A = Alcotest

exception IA

let tests_corrupt =
  let minimal =
    {|
opam-version: "2.1"
version: "2.1"
    |}
  in
  let opamfile = OpamParser.FullPos.string minimal "corrupt.opam" in
  let corrupt = OpamParserTypes.FullPos.({opamfile with file_contents = List.rev opamfile.file_contents}) in
  [
    "OpamPrinter.FullPos.opamfile", OpamPrinter.FullPos.opamfile;
    "OpamPrinter.FullPos.Normalise.opamfile", OpamPrinter.FullPos.Normalise.opamfile
] |> List.map (fun (name, f) ->
    name, (fun () ->
        A.check_raises name IA (fun () ->
            try ignore (f corrupt) with Invalid_argument _ -> raise IA)))

let tests_exn = [
  "opam-version > 2.0 not at start 1", OpamLexer.Error("opam-version must be the first non-comment line"),
  {|
version: "2.1"
opam-version: "2.1"
  |};
  "opam-version > 2.1 repeated", OpamLexer.Error("opam-version cannot be repeated"),
  {|
opam-version: "2.1"
opam-version: "2.1"
  |};
  "no opam-version and parsing error", Parsing.Parse_error,
  {|
build: [ "echo"
  |};
  "opam-version 2.1 and lexing error", OpamLexer.Error "'@' is not a valid token",
  {|
opam-version: "2.1"
@
  |};
  "opam-version 2.1 and parsing error", Parsing.Parse_error,
  {|
opam-version: "2.1"
build: [ "echo"
  |};
  "opam-version 2.1 and immediate parsing error", Parsing.Parse_error,
  {|
opam-version: "2.1"
!!
  |};
] |> List.map (fun (name, exn, content) ->
    name, (fun () ->
        A.check_raises name exn (fun () ->
            OpamParser.FullPos.string content "broken.opam" |> ignore)))

let has_sentinel =
  let open OpamParserTypes.FullPos in
  fun {file_contents; _} ->
    match List.rev file_contents with
    | {pelem = Section {section_kind = {pelem = "#"; _}; _}; _}::_ -> true
    | _ -> false

let tests_noexn = [
  "opam-version 4 and parsing error",
  {|
opam-version: "4"
!!
  |};
  "opam-version 42.0 and parsing error",
  {|
opam-version: "42.0"
version: "42.0"
!!
  |};
  "opam-version 42.0 and evil parsing error",
  {|
opam-version: "42.0" <
  |};
  "opam-version 42.0 and immediate parsing error",
  {|
opam-version: "42.0"
!!
  |};
  "opam-version 42.0 and lexing error",
  {|
opam-version: "42.0"
@
  |};
] |> List.map (fun (name, content) ->
    name, (fun () ->
        A.check A.bool name true
          (OpamParser.FullPos.string content "broken.opam"
           |> has_sentinel)))

let tests =
  ["opam-version", tests_corrupt @ tests_exn @ tests_noexn]