diff --git a/tests_python/tests_014/test_contract.py b/tests_python/tests_014/test_contract.py index d45306444f5495886b5dee68c5aff776c2975362..caa86ec516ee75c454b034635cc6443bd59f4312 100644 --- a/tests_python/tests_014/test_contract.py +++ b/tests_python/tests_014/test_contract.py @@ -8,14 +8,6 @@ from tools.utils import originate from .contract_paths import CONTRACT_PATH -ID_SCRIPT_LITERAL = ''' -parameter unit; storage unit; code {CAR; NIL operation; PAIR} -'''.strip() -ID_SCRIPT_HASH = ''' -exprtpyospPfMqcARmu5FGukprC7kbbe4jb4zxFd4Gxrp2vcCPjRNa -'''.strip() - - @pytest.mark.contract @pytest.mark.incremental class TestManager: @@ -260,79 +252,3 @@ class TestManager: 'manager', 'rooted_target', ['--arg', arg, '--entrypoint', 'root'] ) utils.bake(client, bake_for='bootstrap5') - - -@pytest.mark.contract -class TestScriptHashMultiple: - """Test octez-client hash script with diffent number and type of - arguments""" - - def test_contract_hashes_empty(self, client: Client): - assert client.hash_script([]) == [] - - def test_contract_hashes_single(self, client: Client): - assert client.hash_script([ID_SCRIPT_LITERAL]) == [ - (ID_SCRIPT_HASH, None) - ] - - def test_contract_hashes_single_display_names(self, client: Client): - assert client.hash_script([ID_SCRIPT_LITERAL], display_names=True,) == [ - ( - ID_SCRIPT_HASH, - 'Literal script 1', - ) - ] - - def test_contract_hashes_mixed(self, client: Client): - contract_path = os.path.join(CONTRACT_PATH, 'attic', 'empty.tz') - script_empty_hash = ''' -expruat2BS4KCwn9kbopeX1ZwxtrtJbyFhpnpnG6A5KdCBCwHNsdod - '''.strip() - with open(contract_path, 'r') as contract_file: - script = contract_file.read() - - hashes = client.hash_script([contract_path, script]) - - assert hashes == [ - ( - script_empty_hash, - None, - ), - ( - script_empty_hash, - None, - ), - ] - - hashes = client.hash_script( - [contract_path, script], display_names=True - ) - - assert hashes == [ - ( - script_empty_hash, - contract_path, - ), - ( - script_empty_hash, - 'Literal script 2', - ), - ] - - @pytest.mark.parametrize( - "for_script, display_names, results", - [ - ('csv', True, (ID_SCRIPT_HASH, 'Literal script 1')), - ('csv', False, (ID_SCRIPT_HASH, None)), - ('tsv', True, (ID_SCRIPT_HASH, 'Literal script 1')), - ('tsv', False, (ID_SCRIPT_HASH, None)), - ], - ) - def test_contract_hashes_for_script( - self, client: Client, for_script, display_names, results - ): - assert client.hash_script( - [ID_SCRIPT_LITERAL], - display_names=display_names, - for_script=for_script, - ) == [results] diff --git a/tests_python/tests_015/test_contract.py b/tests_python/tests_015/test_contract.py index d45306444f5495886b5dee68c5aff776c2975362..caa86ec516ee75c454b034635cc6443bd59f4312 100644 --- a/tests_python/tests_015/test_contract.py +++ b/tests_python/tests_015/test_contract.py @@ -8,14 +8,6 @@ from tools.utils import originate from .contract_paths import CONTRACT_PATH -ID_SCRIPT_LITERAL = ''' -parameter unit; storage unit; code {CAR; NIL operation; PAIR} -'''.strip() -ID_SCRIPT_HASH = ''' -exprtpyospPfMqcARmu5FGukprC7kbbe4jb4zxFd4Gxrp2vcCPjRNa -'''.strip() - - @pytest.mark.contract @pytest.mark.incremental class TestManager: @@ -260,79 +252,3 @@ class TestManager: 'manager', 'rooted_target', ['--arg', arg, '--entrypoint', 'root'] ) utils.bake(client, bake_for='bootstrap5') - - -@pytest.mark.contract -class TestScriptHashMultiple: - """Test octez-client hash script with diffent number and type of - arguments""" - - def test_contract_hashes_empty(self, client: Client): - assert client.hash_script([]) == [] - - def test_contract_hashes_single(self, client: Client): - assert client.hash_script([ID_SCRIPT_LITERAL]) == [ - (ID_SCRIPT_HASH, None) - ] - - def test_contract_hashes_single_display_names(self, client: Client): - assert client.hash_script([ID_SCRIPT_LITERAL], display_names=True,) == [ - ( - ID_SCRIPT_HASH, - 'Literal script 1', - ) - ] - - def test_contract_hashes_mixed(self, client: Client): - contract_path = os.path.join(CONTRACT_PATH, 'attic', 'empty.tz') - script_empty_hash = ''' -expruat2BS4KCwn9kbopeX1ZwxtrtJbyFhpnpnG6A5KdCBCwHNsdod - '''.strip() - with open(contract_path, 'r') as contract_file: - script = contract_file.read() - - hashes = client.hash_script([contract_path, script]) - - assert hashes == [ - ( - script_empty_hash, - None, - ), - ( - script_empty_hash, - None, - ), - ] - - hashes = client.hash_script( - [contract_path, script], display_names=True - ) - - assert hashes == [ - ( - script_empty_hash, - contract_path, - ), - ( - script_empty_hash, - 'Literal script 2', - ), - ] - - @pytest.mark.parametrize( - "for_script, display_names, results", - [ - ('csv', True, (ID_SCRIPT_HASH, 'Literal script 1')), - ('csv', False, (ID_SCRIPT_HASH, None)), - ('tsv', True, (ID_SCRIPT_HASH, 'Literal script 1')), - ('tsv', False, (ID_SCRIPT_HASH, None)), - ], - ) - def test_contract_hashes_for_script( - self, client: Client, for_script, display_names, results - ): - assert client.hash_script( - [ID_SCRIPT_LITERAL], - display_names=display_names, - for_script=for_script, - ) == [results] diff --git a/tests_python/tests_alpha/test_contract.py b/tests_python/tests_alpha/test_contract.py index d45306444f5495886b5dee68c5aff776c2975362..caa86ec516ee75c454b034635cc6443bd59f4312 100644 --- a/tests_python/tests_alpha/test_contract.py +++ b/tests_python/tests_alpha/test_contract.py @@ -8,14 +8,6 @@ from tools.utils import originate from .contract_paths import CONTRACT_PATH -ID_SCRIPT_LITERAL = ''' -parameter unit; storage unit; code {CAR; NIL operation; PAIR} -'''.strip() -ID_SCRIPT_HASH = ''' -exprtpyospPfMqcARmu5FGukprC7kbbe4jb4zxFd4Gxrp2vcCPjRNa -'''.strip() - - @pytest.mark.contract @pytest.mark.incremental class TestManager: @@ -260,79 +252,3 @@ class TestManager: 'manager', 'rooted_target', ['--arg', arg, '--entrypoint', 'root'] ) utils.bake(client, bake_for='bootstrap5') - - -@pytest.mark.contract -class TestScriptHashMultiple: - """Test octez-client hash script with diffent number and type of - arguments""" - - def test_contract_hashes_empty(self, client: Client): - assert client.hash_script([]) == [] - - def test_contract_hashes_single(self, client: Client): - assert client.hash_script([ID_SCRIPT_LITERAL]) == [ - (ID_SCRIPT_HASH, None) - ] - - def test_contract_hashes_single_display_names(self, client: Client): - assert client.hash_script([ID_SCRIPT_LITERAL], display_names=True,) == [ - ( - ID_SCRIPT_HASH, - 'Literal script 1', - ) - ] - - def test_contract_hashes_mixed(self, client: Client): - contract_path = os.path.join(CONTRACT_PATH, 'attic', 'empty.tz') - script_empty_hash = ''' -expruat2BS4KCwn9kbopeX1ZwxtrtJbyFhpnpnG6A5KdCBCwHNsdod - '''.strip() - with open(contract_path, 'r') as contract_file: - script = contract_file.read() - - hashes = client.hash_script([contract_path, script]) - - assert hashes == [ - ( - script_empty_hash, - None, - ), - ( - script_empty_hash, - None, - ), - ] - - hashes = client.hash_script( - [contract_path, script], display_names=True - ) - - assert hashes == [ - ( - script_empty_hash, - contract_path, - ), - ( - script_empty_hash, - 'Literal script 2', - ), - ] - - @pytest.mark.parametrize( - "for_script, display_names, results", - [ - ('csv', True, (ID_SCRIPT_HASH, 'Literal script 1')), - ('csv', False, (ID_SCRIPT_HASH, None)), - ('tsv', True, (ID_SCRIPT_HASH, 'Literal script 1')), - ('tsv', False, (ID_SCRIPT_HASH, None)), - ], - ) - def test_contract_hashes_for_script( - self, client: Client, for_script, display_names, results - ): - assert client.hash_script( - [ID_SCRIPT_LITERAL], - display_names=display_names, - for_script=for_script, - ) == [results] diff --git a/tezt/lib_tezos/client.ml b/tezt/lib_tezos/client.ml index 030b1084f1ea5443e31222b90dfab49989d6bb66..9c6ff43fc73932a7182fb87723f1bf70ae110364 100644 --- a/tezt/lib_tezos/client.ml +++ b/tezt/lib_tezos/client.ml @@ -1564,11 +1564,18 @@ let register_global_constant ?wait ?burn_cap ~src ~value client = client_output | Some hash -> return hash -let spawn_hash_script ?hooks ~script client = - spawn_command ?hooks client ["hash"; "script"; script] +type hash_script_format = TSV | CSV -let hash_script ?hooks ~script client = - spawn_hash_script ?hooks ~script client |> Process.check_and_read_stdout +let show_hash_script_format = function TSV -> "tsv" | CSV -> "csv" + +let spawn_hash_script ?hooks ?for_script ~script client = + spawn_command ?hooks client + @@ ["hash"; "script"; script] + @ optional_arg "for-script" show_hash_script_format for_script + +let hash_script ?hooks ?for_script ~script client = + spawn_hash_script ?hooks ?for_script ~script client + |> Process.check_and_read_stdout let spawn_get_contract_hash ?hooks ~contract client = spawn_command @@ -1580,17 +1587,22 @@ let get_contract_hash ?hooks ~contract client = spawn_get_contract_hash ?hooks ~contract client |> Process.check_and_read_stdout -let spawn_hash_scripts ?hooks ?(display_names = false) scripts client = +let spawn_hash_scripts ?hooks ?(display_names = false) ?for_script scripts + client = spawn_command ?hooks client @@ ["hash"; "script"] @ scripts @ optional_switch "display-names" display_names + @ optional_arg "for-script" show_hash_script_format for_script -let hash_scripts ?hooks ?display_names scripts client = +let hash_scripts ?hooks ?display_names ?for_script scripts client = let* output = - spawn_hash_scripts ?hooks ?display_names scripts client + spawn_hash_scripts ?hooks ?display_names ?for_script scripts client |> Process.check_and_read_stdout in - return (String.split_on_char '\n' output) + return + (match String.trim output with + | "" -> [] + | output -> String.split_on_char '\n' output) let spawn_hash_data ?hooks ~data ~typ client = let cmd = ["hash"; "data"; data; "of"; "type"; typ] in diff --git a/tezt/lib_tezos/client.mli b/tezt/lib_tezos/client.mli index ad9d9843ea1315f5778a250626fc6b3c8c658839..4c3a364e620a7a53c0d30383eaae1b4141fd2f25 100644 --- a/tezt/lib_tezos/client.mli +++ b/tezt/lib_tezos/client.mli @@ -1243,12 +1243,24 @@ val hash_data : val spawn_hash_data : ?hooks:Process.hooks -> data:string -> typ:string -> t -> Process.t +(** Arguments to the [hash_script]'s and [hash_data]'s [?for_script] *) +type hash_script_format = TSV | CSV + (** Run [octez-client hash script ..]*) -val hash_script : ?hooks:Process_hooks.t -> script:string -> t -> string Lwt.t +val hash_script : + ?hooks:Process_hooks.t -> + ?for_script:hash_script_format -> + script:string -> + t -> + string Lwt.t (** Same as [hash_script], but do not wait for the process to exit. *) val spawn_hash_script : - ?hooks:Process_hooks.t -> script:string -> t -> Process.t + ?hooks:Process_hooks.t -> + ?for_script:hash_script_format -> + script:string -> + t -> + Process.t (** Run [octez-client get contract script hash for ..]*) val get_contract_hash : @@ -1262,13 +1274,19 @@ val spawn_get_contract_hash : val hash_scripts : ?hooks:Process_hooks.t -> ?display_names:bool -> + ?for_script:hash_script_format -> string list -> t -> string list Lwt.t (** Same as [hash_scripts], but do not wait for the process to exit. *) val spawn_hash_scripts : - ?hooks:Process_hooks.t -> ?display_names:bool -> string list -> t -> Process.t + ?hooks:Process_hooks.t -> + ?display_names:bool -> + ?for_script:hash_script_format -> + string list -> + t -> + Process.t (** Run [octez-client normalize data .. of type ...]*) val normalize_data : diff --git a/tezt/tests/main.ml b/tezt/tests/main.ml index 84f38acf1db716b52333cced73fd123e3b5beb23..b0988712c24a7ac0b793986a10b1cc660e2de662 100644 --- a/tezt/tests/main.ml +++ b/tezt/tests/main.ml @@ -157,6 +157,7 @@ let register_protocol_tests_that_use_supports_correctly () = Script_chain_id.register ~protocols ; Script_execution_ordering.register ~protocols ; Script_hash_regression.register ~protocols ; + Script_hash_multiple.register ~protocols ; Self_address_transfer.register ~protocols ; Script_conversion.register ~protocols ; Script_illtyped.register ~protocols ; diff --git a/tezt/tests/script_hash_multiple.ml b/tezt/tests/script_hash_multiple.ml new file mode 100644 index 0000000000000000000000000000000000000000..67c760cfd6c77ed4bb947f3071ba5051c11620e0 --- /dev/null +++ b/tezt/tests/script_hash_multiple.ml @@ -0,0 +1,147 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2020 Nomadic Labs *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +(* Testing + ------- + Component: Client's hash script command + Invocation: dune exec tezt/tests/main.exe -- --file script_hash_multiple.ml + Subject: Tests various invocations of the [octez-client hash script] +*) + +let contract_path protocol kind contract = + sf + "tests_python/contracts_%s/%s/%s" + (match protocol with + | Protocol.Alpha -> "alpha" + | _ -> sf "%03d" @@ Protocol.number protocol) + kind + contract + +(* Test octez-client hash script with different number and type of + arguments *) +let test_script_hash_multiple = + Protocol.register_test + ~__FILE__ + ~title:"Script hash multiple" + ~tags:["script"; "hash"; "multiple"] + @@ fun protocol -> + let id_script_path = contract_path protocol "attic" "empty.tz" in + let id_script_literal = Base.read_file id_script_path in + let id_script_hash = + "expruat2BS4KCwn9kbopeX1ZwxtrtJbyFhpnpnG6A5KdCBCwHNsdod" + in + let* client = Client.init_mockup ~protocol () in + Log.info "Contract hashes empty" ; + let* hashes_empty = Client.hash_scripts [] client in + Check.( + (hashes_empty = []) + (list string) + ~__LOC__ + ~error_msg:"Expected an empty list, got %L") ; + Log.info "Contract hashes single" ; + let* hashes_single = Client.hash_scripts [id_script_literal] client in + Check.( + (hashes_single = [id_script_hash]) + (list string) + ~__LOC__ + ~error_msg:"Expected single hash %R, got %L") ; + Log.info "Contract hashes single display names" ; + let* hashes_single = + Client.hash_scripts ~display_names:true [id_script_literal] client + in + let expected_output_line ?(format = Client.TSV) ?name hash = + match (format, name) with + | CSV, Some name -> sf "%S,%S" hash name + | CSV, None -> sf "%S" hash + | TSV, Some name -> sf "%s\t%s" hash name + | TSV, None -> hash + in + Check.( + (hashes_single + = [expected_output_line ~name:"Literal script 1" id_script_hash]) + (list string) + ~__LOC__ + ~error_msg:"Expected %R, got %L") ; + Log.info "Contract hashes mixed" ; + let* hashes_mixed = + Client.hash_scripts [id_script_path; id_script_literal] client + in + Check.( + (hashes_mixed = [id_script_hash; id_script_hash]) + (list string) + ~__LOC__ + ~error_msg:"Expected %R, got %L") ; + let* hashes_mixed_named = + Client.hash_scripts + ~display_names:true + [id_script_path; id_script_literal] + client + in + Check.( + (hashes_mixed_named + = [ + expected_output_line ~name:id_script_path id_script_hash; + expected_output_line ~name:"Literal script 2" id_script_hash; + ]) + (list string) + ~__LOC__ + ~error_msg:"Expected %R, got %L") ; + Log.info "Contract hashes for script" ; + let* () = + Client. + [ + ( CSV, + true, + expected_output_line + ~format:CSV + ~name:"Literal script 1" + id_script_hash ); + (CSV, false, expected_output_line ~format:CSV id_script_hash); + ( TSV, + true, + expected_output_line + ~format:TSV + ~name:"Literal script 1" + id_script_hash ); + (TSV, false, expected_output_line ~format:TSV id_script_hash); + ] + |> Lwt_list.iter_s (fun (for_script, display_names, expected_results) -> + let* hashes = + Client.hash_scripts + ~display_names + ~for_script + [id_script_literal] + client + in + Check.( + (hashes = [expected_results]) + (list string) + ~__LOC__ + ~error_msg:"Expected %R, got %L") ; + unit) + in + unit + +let register ~protocols = test_script_hash_multiple protocols