Programming Elixir, chapter 13, test issue

I’ve been following along with the book and typing in the example application of Chapter 13.

My problem is in running the “mix text” from page 162. It always fails because the ‘atom-elixir-formatter’ v1.0.3 for the Atom editor does too good of a job and removes the trailing spaces of the text block used by the ‘assert’ of the “Output is correct” test.

Is there any way to tell the ‘atom-elixir-formatter’, from inside the file’s source code, to temporarily not remove trailing spaces? Or perhaps tell the formatter to skip a specific set of lines?

I know I could always temporarily disable the Atom plugin but, knowing me, I’ll forget to re-enable it when I’m done confirming that all tests pass.

For the time being, I just visually compared the left/right differences that the test discovered and ensured that it was only the trailing spaces that weren’t matching the left and right sides of the assert.

Thanks for your time!

I do not know that plugin as I do not use atom, but if it is using the elixir formatter, then you have no chance but to disable it.

But if though by “text block” you actually mean a string, then there shouldn’t get anything removed inside, as it would change the semantic of the code.

PS: I do not have the book as well.

I think this is the code you are referring to, which means you think the formatter is removing trailing spaces from within a heredoc (multi-line string).

I think that the position of the final quotes affects leading spaces, so make sure they are indented correctly.

Can you post your exact code?

test "Output is correct" do
  result = capture_io fn ->
    TF.print_table_for_columns(simple_test_data, headers)
  end
  assert result == """
    c1 | c2 | c4
    ------+--------+--------
    r1 c1 | r1 c2 | r1+++c4
    r2 c1 | r2 c2 | r2 c4
    r3 c1 | r3 c2 | r3 c4
    r4 c1 | r4++c2| r4 c4
    """
end

EDIT: I was not able to get this test to pass, due to ElixirLS extension in VSCode running the formatter and stripping trailing whitespace from the end of "hello " and "world " in the heredoc:

test "test something" do
  string = "hello \nworld \n"

  heredoc = """
  hello 
  world 
  """

  assert string == heredoc
end

EDIT 2: But this works:

test "test something" do
  string = "hello \nworld \n"

  heredoc = """
  hello\s
  world\s
  """

  assert string == heredoc
end

Yep - that’s the code!

That’s interesting that you used a regex symbol within the heredoc and its even more interesting that the ‘system’ bought it!

I’ll give it a try - Thanks!

1 Like

That’s not elixirls or the formatter, that’s probably VScode itself.

As I already said, the formatter will not change semantics of code.

I’m also pretty sure that in the atom case, it’s atom stripping trailing whitespace, as most editors simply strip trailing whitespace without even knowing about semantics.

2 Likes

It doesn’t look like Atom is the culprit - Atom has no preference setting regarding the stripping, but having said that, I just created a test text file (.txt to ensure that the formatter plugin wouldn’t touch it) and it saved with my trailing spaces.

I think I’m going to report the problem to the plugin author - maybe its an oversight!?

IMHO: The formatter should not be touching text inside of a heredoc.

Thanks for looking into this for me!

2 Likes

As you can see in this transcript, the formatter does not strip trailing whitespace in strings:

$ xxd lib/foo.ex
00000000: 6465 666d 6f64 756c 6520 466f 6f20 646f  defmodule Foo do
00000010: 0a20 2064 6566 2068 656c 6c6f 2064 6f0a  .  def hello do.
00000020: 2020 2020 2222 220a 2020 2020 5768 6974      """.    Whit
00000030: 6573 7061 6365 2074 6573 7420 2020 2020  espace test     
00000040: 200a 2020 2020 2222 220a 2020 656e 640a   .    """.  end.
00000050: 656e 640a                                end.
$ mix format
$ xxd lib/foo.ex
00000000: 6465 666d 6f64 756c 6520 466f 6f20 646f  defmodule Foo do
00000010: 0a20 2064 6566 2068 656c 6c6f 2064 6f0a  .  def hello do.
00000020: 2020 2020 2222 220a 2020 2020 5768 6974      """.    Whit
00000030: 6573 7061 6365 2074 6573 7420 2020 2020  espace test     
00000040: 200a 2020 2020 2222 220a 2020 656e 640a   .    """.  end.
00000050: 656e 640a                                end.

The file is the same before and after the run.

1 Like

Interesting. I made sure to change VSCode settings to not strip trailing whitespace before testing. With the ElixirLS extension disabled whitespace would not be stripped. As soon as I enabled it, whitespace would be stripped. I assume that the extension is just using the Elixir formatter. I will test again using the formatter directly instead of the extension.

EDIT: @NobbZ is correct, the elixir formatter is not doing the whitespace stripping in my case. Although I had this setting in VSCode "files.trimTrailingWhitespace": false, the ElixirLS extension injected this into my settings:

  // Configure editor settings to be overridden for [elixir] language.
  "[elixir]": {
    "editor.insertSpaces": true,
    "editor.tabSize": 2,
    "editor.wordBasedSuggestions": false,
    "editor.formatOnType": true,
    "editor.acceptSuggestionOnEnter": "off",
    "editor.trimAutoWhitespace": false,
    "files.trimTrailingWhitespace": true,
    "files.insertFinalNewline": true
  }

Maybe the Elixir extension in atom is doing something similar?

Indeed, it was Atom! Sorry for the false alarm. Thanks everyone for looking into the problem.

1 Like

No problem :slightly_smiling_face: I learnt that ElixirLS is overwriting my settings for trailing whitespace and have opened a Github issue for this. Thanks to @NobbZ I also learnt about xxd which I think could be quite useful.