Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iodata handling #105

Closed
JonasGruenwald opened this issue Dec 15, 2024 · 5 comments
Closed

iodata handling #105

JonasGruenwald opened this issue Dec 15, 2024 · 5 comments

Comments

@JonasGruenwald
Copy link

Hello! I noticed when trying to snapshot the body of an outgoing HTTP request, that mneme will crash with a kind of vague error when given iodata.

Reproduction case:

  test "iodata snapshot" do
    wibble = %{
      "wobble" => 1
    }
    |> Jason.encode_to_iodata!()
    auto_assert wibble
  end

Error:

1) test iodata snapshot (Backend.SomeTest)
   test/backend/some_test.exs:6
   ** (Mneme.InternalError) Mneme encountered an internal error. This is likely a bug in Mneme.

   Please consider reporting this error at https://github.com/zachallaun/mneme/issues. Thanks!

   ** (FunctionClauseError) no function clause matching in Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
       (elixir 1.17.3) lib/enum.ex:1829: Enum."-map_reduce/3-lists^mapfoldl/2-0-"(#Function<3.54260353/2 in Mneme.Assertion.PatternBuilder.enum_to_patterns/3>, [], "wobble")
       (elixir 1.17.3) lib/enum.ex:1829: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
       (mneme 0.10.0) lib/mneme/assertion/pattern_builder.ex:281: Mneme.Assertion.PatternBuilder.enum_to_patterns/3
       (mneme 0.10.0) lib/mneme/assertion/pattern_builder.ex:78: Mneme.Assertion.PatternBuilder.do_to_patterns/3
       (mneme 0.10.0) lib/mneme/assertion/pattern_builder.ex:21: Mneme.Assertion.PatternBuilder.to_patterns/3
       (elixir 1.17.3) lib/enum.ex:1829: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
       (elixir 1.17.3) lib/enum.ex:1829: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
       (mneme 0.10.0) lib/mneme/assertion/pattern_builder.ex:281: Mneme.Assertion.PatternBuilder.enum_to_patterns/3
       (mneme 0.10.0) lib/mneme/assertion/pattern_builder.ex:78: Mneme.Assertion.PatternBuilder.do_to_patterns/3
       (mneme 0.10.0) lib/mneme/assertion/pattern_builder.ex:21: Mneme.Assertion.PatternBuilder.to_patterns/3
       (mneme 0.10.0) lib/mneme/assertion/pattern_builder.ex:16: Mneme.Assertion.PatternBuilder.to_patterns/2
       (mneme 0.10.0) lib/mneme/assertion.ex:360: Mneme.Assertion.build_and_select_pattern/2
       (mneme 0.10.0) lib/mneme/assertion.ex:301: Mneme.Assertion.generate_patterns/1
       (mneme 0.10.0) lib/mneme/patcher.ex:142: Mneme.Patcher.prepare_assertion/2
       (mneme 0.10.0) lib/mneme/patcher.ex:66: Mneme.Patcher.patch!/3
       (elixir 1.17.3) lib/task/supervised.ex:101: Task.Supervised.invoke_mfa/2
       (elixir 1.17.3) lib/task/supervised.ex:36: Task.Supervised.reply/4

   
   code: auto_assert wibble
   stacktrace:
     (mneme 0.10.0) lib/mneme/assertion.ex:242: Mneme.Assertion.handle_assertion/4
     (mneme 0.10.0) lib/mneme/assertion.ex:179: Mneme.Assertion.run!/3
     test/backend/some_test.exs:11: (test)

The error is also the same if the iodata is not passed directly to auto_assert but inside of a struct/map.

Would be great if mneme could automatically convert iodata to string like IO.inspect does and snapshot the string, or otherwise would be nice to have an error that's more clear about what's wrong with the given input data.

@zachallaun
Copy link
Owner

zachallaun commented Dec 15, 2024

Thanks for the report! This is definitely a bug -- something in Mneme's pattern generation doesn't like this. If you're able to IO.inspect the iodata and share it here, that will be helpful to reproduce.

In the meantime, I'd recommend to_string to assert on it, but I'll get this fixed asap.

@zachallaun
Copy link
Owner

I'm silly, you have a simple example there already. Will get it fixed.

@zachallaun
Copy link
Owner

@JonasGruenwald Would you mind giving main a shot and making sure this is now working as expected?

{:mneme, github: "zachallaun/mneme", ref: "main"}

@JonasGruenwald
Copy link
Author

Yeah that works, thanks for your quick work @zachallaun !

@zachallaun
Copy link
Owner

zachallaun commented Dec 17, 2024

Great to hear! I'll cut a release shortly. (Edit: Included in v0.10.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants