Skip to content

Commit

Permalink
Enable keeping last empty line for win_lineinfile (ansible-collection…
Browse files Browse the repository at this point in the history
…s#219)

* Keep last empty line for win_lineinfile

Ensure that an empty last line is still be present in the output file
when changed.

* Keep whitespace consistent

* Added changelog fragment

Co-authored-by: Daniel Blixt <daniel.blixt.2@volvocars.com>
Co-authored-by: Jordan Borean <jborean93@gmail.com>
  • Loading branch information
3 people authored Jun 19, 2021
1 parent 1120f0c commit 0cc0319
Show file tree
Hide file tree
Showing 39 changed files with 332 additions and 48 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/win_lineinfile-newline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- win_lineinfile - Avoid stripping the newline at the end of a file - https://github.com/ansible-collections/community.windows/pull/219
14 changes: 14 additions & 0 deletions plugins/modules/win_lineinfile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function Present($path, $regex, $line, $insertafter, $insertbefore, $create, $ba
# Note that we have to clean up the path because ansible wants to treat / and \ as
# interchangeable in windows pathnames, but .NET framework internals do not support that.
$cleanpath = $path.Replace("/", "\");
$endswithnewline = $null

# Check if path exists. If it does not exist, either create it if create == "yes"
# was specified or fail with a reasonable error message.
Expand All @@ -76,6 +77,7 @@ function Present($path, $regex, $line, $insertafter, $insertbefore, $create, $ba
}
# Create new empty file, using the specified encoding to write correct BOM
[System.IO.File]::WriteAllLines($cleanpath, "", $encodingobj);
$endswithnewline = $false
}

# Initialize result information
Expand All @@ -96,6 +98,10 @@ function Present($path, $regex, $line, $insertafter, $insertbefore, $create, $ba
}
Else {
$lines = [System.Collections.ArrayList] $before;
If ($null -eq $endswithnewline ) {
$alltext = [System.IO.File]::ReadAllText($cleanpath, $encodingobj);
$endswithnewline = (($alltext[-1] -eq "`n") -or ($alltext[-1] -eq "`r"))
}
}

if ($diff_support) {
Expand Down Expand Up @@ -195,6 +201,10 @@ function Present($path, $regex, $line, $insertafter, $insertbefore, $create, $ba
$result.backup = $result.backup_file
}

if ($endswithnewline) {
$lines.Add("")
}

$writelines_params = @{
outlines = $lines
path = $path
Expand Down Expand Up @@ -241,6 +251,10 @@ function Absent($path, $regex, $line, $backup, $validate, $encodingobj, $linesep
}
Else {
$lines = [System.Collections.ArrayList] $before;
$alltext = [System.IO.File]::ReadAllText($cleanpath, $encodingobj);
If (($alltext[-1] -eq "`n") -or ($alltext[-1] -eq "`r")) {
$lines.Add("")
}
}

if ($diff_support) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.text text eol=LF
*.txt text eol=CRLF
*.txt16 text working-tree-encoding=UTF-16 eol=CRLF
*.txt32 text working-tree-encoding=UTF-32 eol=CRLF
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
New line at the beginning
This is line 1
This is line 2
REF this is a line for backrefs REF
This is line 4
This is line 5
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
New line at the beginning
This is line 1
This is line 2
REF this is a line for backrefs REF
This is line 4
This is line 5
New line at the end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
New line at the beginning
This is line 1
New line after line 1
This is line 2
REF this is a line for backrefs REF
This is line 4
This is line 5
New line at the end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
New line at the beginning
This is line 1
New line after line 1
This is line 2
REF this is a line for backrefs REF
This is line 4
New line before line 5
This is line 5
New line at the end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
New line at the beginning
This is line 1
New line after line 1
This is line 2
This is line 3
This is line 4
New line before line 5
This is line 5
New line at the end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
New line at the beginning
This is line 1
New line after line 1
This is line 2
This is line 4
New line before line 5
This is line 5
New line at the end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
New line at the beginning
This is line 1
New line after line 1
This is line 2
This is line 4
New line before line 5
New line at the end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
New line at the beginning
This is line 1
New line after line 1
This is line 2
This is line 4
New line before line 5
New line at the end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a new file
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is line 1
This is line 2
New line at the end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
New line at the beginning
This is line 1
New line after line 1
This is line 2
This is line 4
New line before line 5
New line at the end
This is a line
with newline character
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
New line at the end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
New line at the beginning
This is line 1
New line after line 1
This is line 2
New line 4 created with the backref
New line before line 5
New line at the end
This is a line
with newline character
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var dotenv = require('dotenv');
dotenv.load();
'foo'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var dotenv = require('dotenv');
dotenv.load();
'foo'
import g'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var dotenv = require('dotenv');
dotenv.load();
'foo'
import g'
"quote" and "unquote"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a new file
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is a new file
This is the last line
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a new file
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is a new file
This is the last line
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a new utf-8 file
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is a new utf-8 file
This is the last line
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a new utf-8 file
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is a new utf-8 file
This is the last line
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a new utf-16 file
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is a new utf-16 file
This is the last line
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a new utf-32 file
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is a new utf-32 file
This is the last line
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c:\return\new
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
c:\return\new
c:eturn
ew
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
New line at the beginning
This is line 1
This is line 2
REF this is a line for backrefs REF
This is line 4
This is line 5
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
***WIN LINEINFILE Expectations***

This folder contains expected files as the tests in this playbook executes on the
files in 'files'.

To get the checksum as would win_stat in the tests, go to this folder in powershell and
execute

```powershell
Get-ChildItem | ForEach-Object {
$fp = [System.IO.File]::Open("$pwd/$($_.Name)", [System.IO.Filemode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite)
Write-Output $_.Name
try {
[System.BitConverter]::ToString($sp.ComputeHash($fp)).Replace("-", "").ToLower()
} finally {
$fp.Dispose()
}
Write-Output ""
}
```

There is one exception right now: 30_linebreaks_checksum_bad.txt which requires mixed line endings that
git cannot handle without turning the file binary. The file should read

```
c:\return\newCRLF
c:CR
eturnLF
ew
```
where CR and LF denote carriage return (\r) and line feed (\n) respectively, to get the correct checksum.

Also, the .gitattributes files is important as it assures that the EOL characters
for the files are correct, regardless of environment. The files may be checked out on
linux but the resulting files will be created using windows EOL, and the comparison must
match.
Loading

0 comments on commit 0cc0319

Please sign in to comment.