Skip to content

Commit

Permalink
docs: add info
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jan 5, 2025
1 parent 17d93d8 commit 802b9a6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ consider setting up neotest and its adapters in a
| `warn_test_name_dupes` | `true` | Warn about duplicate test names within the same Go package. |
| `warn_test_not_executed` | `true` | Warn if test was not executed. |
| `log_level` | `vim.log.levels.WARN` | Log level. |
| `sanitize_output` | `false` | Filter control characters and non-printable characters from test output. Note: [usage](example-configuration-sanitize-output). |

> [!NOTE]
>
Expand Down Expand Up @@ -382,6 +383,51 @@ require("neotest").setup({
})
```

### Example configuration: sanitize output

When tests write non-printable characters to stdout/stderr, they can cause
various issues like failing to write output to disk or UI rendering problems.
The `sanitize_output` option helps clean up such output by preserving UTF-8 and
replacing control characters with the Unicode replacement character (�).

This is particularly useful when:

- Tests write bytes to stdout/stderr.
- Test output contains terminal control sequences.
- Test output includes non-printable characters.

The sanitization preserves all regular printable characters including tabs,
newlines, and carriage returns.

```diff
return {
{
"nvim-neotest/neotest",
dependencies = {
"nvim-neotest/nvim-nio",
"nvim-lua/plenary.nvim",
"antoinemadec/FixCursorHold.nvim",
"nvim-treesitter/nvim-treesitter",
- "fredrikaverpil/neotest-golang", -- Installation
+ {
+ "fredrikaverpil/neotest-golang", -- Installation
+ dependencies = {
+ "uga-rosa/utf8.nvim", -- Additional dependency required
+ },
+ },
},
config = function()
require("neotest").setup({
adapters = {
- require("neotest-golang"), -- Registration
+ require("neotest-golang")({ sanitize_output = true }), -- Registration
},
})
end,
},
}
```

### Example configuration: extra everything

In the below code block, I've provided a pretty hefty configuration example,
Expand Down
11 changes: 11 additions & 0 deletions lua/neotest-golang/lib/sanitize.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ local function isSequentialList(t)
return true
end

--- Sanitize a string by removing non-printable characters.
--- - `utf8.codes()` iterates over complete UTF-8 characters,
--- regardless of how many bytes they use (1-4 bytes per character)
--- - `utf8.codepoint()` correctly extracts the Unicode code point
--- from a complete UTF-8 sequence
--- - `utf8.char()` properly converts a code point back into the
--- correct UTF-8 byte sequence
---
--- This leverages https://github.com/uga-rosa/utf8.nvim
---@param str string
---@return string
function M.sanitize_string(str)
local utf8 = require("utf8")
local sanitized_string = ""
Expand Down

0 comments on commit 802b9a6

Please sign in to comment.