Repository: sverweij/dependency-cruiser
Pull request #879 (2023-11-28 16:31:26)

Ensure trailing newline is written to files and stdout
Ensure that a trailing newline is written to files and `stdout`.

## Description

When writing non-binary output, ensure that a trailing newline is written to files and `stdout`.

## Motivation and Context

It would be nice to have a trailing newline in the `.dependency-cruiser-known-violations.json` file generated by `depcruise-baseline` for less friction when the file is subsequently consumed by other tools. I believe that something similar applies to other report files generated by dependency cruiser.

## How Has This Been Tested?

I ran `depcruise-baseline` as a manual test and verified that an additional line feed character gets added to the generated file as a result of this change.

I could look into adding an automated test for that, but I have not done that so far. Please let me know in case it is desired.

One of the existing test files needed to be adapted to capture this change, so in that sense it is somewhat covered by existing unit tests.

- [ ] green ci

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] Documentation only change
- [x] Refactor (non-breaking change which fixes an issue without changing functionality)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [x] :book:

  - My change doesn't require a documentation update, or ...
  - it _does_ and I have updated it

- [x] :balance_scale:
  - The contribution will be subject to [The MIT license](https://github.com/sverweij/dependency-cruiser/blob/main/LICENSE), and I'm OK with that.
  - The contribution is my own original work.
  - I am ok with the stuff in [**CONTRIBUTING.md**](https://github.com/sverweij/dependency-cruiser/blob/main/.github/CONTRIBUTING.md).


codeclimate[bot]: Code Climate has analyzed commit 3d5317fe and detected **0 issues** on this pull request.

View more on [Code Climate](https://codeclimate.com/github/sverweij/dependency-cruiser/pull/879).

codeclimate[bot]: Code Climate has analyzed commit e5c53e20 and detected **0 issues** on this pull request.

View more on [Code Climate](https://codeclimate.com/github/sverweij/dependency-cruiser/pull/879).

codeclimate[bot]: Code Climate has analyzed commit b101114b and detected **0 issues** on this pull request.

View more on [Code Climate](https://codeclimate.com/github/sverweij/dependency-cruiser/pull/879).

martinslota: In my view the change doesn't need to be documented, so I checked off that tick box.

Patch:
diff --git a/src/cli/utl/io.mjs b/src/cli/utl/io.mjs
index 13bc06c4..4875e5b5 100644
--- a/src/cli/utl/io.mjs
+++ b/src/cli/utl/io.mjs
@@ -35,15 +35,18 @@ function writeToStdOut(pString, pBufferSize = PIPE_BUFFER_SIZE) {
     const lChunkStart = lIndex * pBufferSize;
     process.stdout.write(
       pString.substring(lChunkStart, lChunkStart + pBufferSize),
-      "utf8"
+      "utf8",
     );
   }
 }
 export function write(pOutputTo, pContent) {
+  const lContentWithTrailingNewline = pContent.endsWith("\n")
+    ? pContent
+    : `${pContent}\n`;
   if ("-" === pOutputTo) {
-    writeToStdOut(pContent);
+    writeToStdOut(lContentWithTrailingNewline);
   } else {
-    writeToFile(pOutputTo, pContent);
+    writeToFile(pOutputTo, lContentWithTrailingNewline);
   }
 }
 


Test patch:
diff --git a/test/cli/__fixtures__/cjs.dir.filtered.html b/test/cli/__fixtures__/cjs.dir.filtered.html
index d3715f5d..58474869 100644
--- a/test/cli/__fixtures__/cjs.dir.filtered.html
+++ b/test/cli/__fixtures__/cjs.dir.filtered.html
@@ -267,4 +267,4 @@ test/cli/__fixtures__/cjs/two_only_one.js -> test/cli/__fixtures__/cjs/sub/dir.j
   </table>
 
 </body>
-</html>
\ No newline at end of file
+</html>