From 09c8953976a7c525abb105d0836796f92fdb2e91 Mon Sep 17 00:00:00 2001 From: Andreas Rossberg Date: Tue, 25 Apr 2023 14:30:45 +0200 Subject: [PATCH] Avoid duplicate module name in test and improve diagnostics --- interpreter/script/run.ml | 23 +++++++++++++---------- test/core/annotations.wast | 4 ++-- test/core/run.py | 11 +++++------ 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/interpreter/script/run.ml b/interpreter/script/run.ml index e0019d84a0..583bd2a22f 100644 --- a/interpreter/script/run.ml +++ b/interpreter/script/run.ml @@ -303,11 +303,14 @@ let modules : Ast.module_ Map.t ref = ref Map.empty let instances : Instance.module_inst Map.t ref = ref Map.empty let registry : Instance.module_inst Map.t ref = ref Map.empty -let bind map x_opt y = +let bind category map x_opt y = let map' = match x_opt with | None -> !map - | Some x -> Map.add x.it y !map + | Some x -> + if Map.mem x.it !map then + IO.error x.at (category ^ " " ^ x.it ^ " already defined"); + Map.add x.it y !map in map := Map.add "" y map' let lookup category map x_opt at = @@ -518,13 +521,13 @@ let rec run_command cmd = print_module x_opt m end end; - bind scripts x_opt [cmd]; - bind modules x_opt m; + bind "module" modules x_opt m; + bind "script" scripts x_opt [cmd]; if not !Flags.dry then begin trace "Initializing..."; let imports = Import.link m in let inst = Eval.init m imports in - bind instances x_opt inst + bind "instance" instances x_opt inst end | Register (name, x_opt) -> @@ -556,17 +559,17 @@ and run_meta cmd = match cmd.it with | Script (x_opt, script) -> run_quote_script script; - bind scripts x_opt (lookup_script None cmd.at) + bind "script" scripts x_opt (lookup_script None cmd.at) | Input (x_opt, file) -> (try if not (input_file file run_quote_script) then Abort.error cmd.at "aborting" with Sys_error msg -> IO.error cmd.at msg); - bind scripts x_opt (lookup_script None cmd.at); + bind "script" scripts x_opt (lookup_script None cmd.at); if x_opt <> None then begin - bind modules x_opt (lookup_module None cmd.at); + bind "module" modules x_opt (lookup_module None cmd.at); if not !Flags.dry then begin - bind instances x_opt (lookup_instance None cmd.at) + bind "instance" instances x_opt (lookup_instance None cmd.at) end end @@ -588,7 +591,7 @@ and run_quote_script script = let save_quote = !quote in quote := []; (try run_script script with exn -> quote := save_quote; raise exn); - bind scripts None (List.rev !quote); + bind "script" scripts None (List.rev !quote); quote := !quote @ save_quote let run_file file = input_file file run_script diff --git a/test/core/annotations.wast b/test/core/annotations.wast index ce538d9371..865581c68a 100644 --- a/test/core/annotations.wast +++ b/test/core/annotations.wast @@ -117,7 +117,7 @@ ) (@a) ) (@a) -((@a) module (@a) $m (@a) (@a) +((@a) module (@a) $m1 (@a) (@a) ((@a) global (@a) $g (@a) ((@a) export (@a) "g" (@a)) (@a) ((@a) import (@a) "spectest" (@a) "global_i32" (@a)) (@a) @@ -142,7 +142,7 @@ ) (@a) ) (@a) -((@a) module (@a) $m (@a) (@a) +((@a) module (@a) $m2 (@a) (@a) ((@a) type (@a) $T (@a) ((@a) func (@a) ((@a) param (@a) i32 (@a) i64 (@a)) (@a) diff --git a/test/core/run.py b/test/core/run.py index 62a99c6b42..fa80ea969b 100755 --- a/test/core/run.py +++ b/test/core/run.py @@ -47,11 +47,10 @@ class RunTests(unittest.TestCase): def _runCommand(self, command, logPath, expectedExitCode = 0): with open(logPath, 'w+') as out: exitCode = subprocess.call(command, shell=True, stdout=out, stderr=subprocess.STDOUT) - if exitCode != expectedExitCode: - print("=== log ===") - subprocess.call("cat %s" % logPath, shell=True) - print("===========") - self.assertEqual(expectedExitCode, exitCode, "failed with exit code %i (expected %i) for %s" % (exitCode, expectedExitCode, command)) + with open(logPath) as out: + log = out.read() + msg = "failed with exit code %i (expected %i)\nCommand:\n %s\nLog:\n%s" + self.assertEqual(expectedExitCode, exitCode, msg % (exitCode, expectedExitCode, command, log)) def _auxFile(self, path): if os.path.exists(path): @@ -64,7 +63,7 @@ def _compareFile(self, expectFile, actualFile): with open(actualFile) as actual: expectText = expect.read() actualText = actual.read() - self.assertEqual(expectText, actualText) + self.assertEqual(expectText, actualText) def _runTestFile(self, inputPath): dir, inputFile = os.path.split(inputPath)