bondiano / mockth

mockth
Apache License 2.0
6 stars 2 forks source link

Can't load module 'erlang' that resides in sticky dir #1

Closed wilsonsilva closed 3 months ago

wilsonsilva commented 3 months ago

The app crashes when I try to mock a function from the package gleam_erlang.

import gleam/erlang.{type GetLineError}
import gleam/io

pub fn main() {
  meet_and_greet()
}

pub fn meet_and_greet() {
  read_name()
  |> greet
}

pub fn read_name() -> Result(String, GetLineError) {
  erlang.get_line("Enter your name: ")
}

pub fn greet(name_result: Result(String, GetLineError)) {
  case name_result {
    Ok("\n") -> io.println("Hello, stranger")
    Ok(name) -> io.println("Hello, " <> name)
    Error(_) -> io.println("Error reading from STDIN")
  }
}
import gleeunit
import mockth

pub fn main() {
  gleeunit.main()
}

pub fn meet_and_greet_test() {
  let assert Ok(_) = mockth.expect("erlang", "get_line", fn(_) { Ok("Wilson") })
}
$ gleam test
  Compiling app
   Compiled in 0.18s
    Running app_test.main
=ERROR REPORT==== 4-Apr-2024::22:10:07.627157 ===
Can't load module 'erlang' that resides in sticky dir

=CRASH REPORT==== 4-Apr-2024::22:10:07.627191 ===
  crasher:
    initial call: meck_proc:init/1
    pid: <0.98.0>
    registered_name: []
    exception exit: {module_is_sticky,erlang}
      in function  gen_server:init_it/6 (gen_server.erl, line 961)
    ancestors: [<0.96.0>]
    message_queue_len: 0
    messages: []
    links: []
    dictionary: []
    trap_exit: true
    status: running
    heap_size: 75113
    stack_size: 28
    reductions: 223445
  neighbours:

F
Failures:

  1) app_test.meet_and_greet_test: module 'app_test'
     No case clause matched
     Value: Error(ModuleIsSticky(Erlang))
     stacktrace:
       meck_proc.set_expect
       meck.expect
       mockth.'-expect/3-fun-0-'
       app_test.meet_and_greet_test
     output:

Finished in 0.365 seconds
1 tests, 1 failures
bondiano commented 3 months ago

The original Meck library has a limitation when it comes to mocking Erlang’s built-in modules. Please refer to https://github.com/eproxus/meck?tab=readme-ov-file#global-namespace for more information. I suggest mocking your own functions instead of Erlang’s built-in ones.