-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Address file.managed with contents_newline flag #54929
Conversation
# Create a file named foo with contents as above but with a \n at EOF | ||
self.run_state('file.managed', name=name, contents=contents, | ||
contents_newline=True) | ||
with salt.utils.files.fopen(name, 'r') as fp_: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be appropriate to open this in rb
mode instead? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, you aren't wrong! I must have forgotten the b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually no I believe it should remain as r. I thought about this and the PR was incorrectly named (definitely my fault) but opening the file in binary mode and then reading it will produce different results based on the platform, i.e. Window's will read the contents differently than a linux platform. So I decided to just open the file in r as it's more in-line with the change in logic itself. Does that make sense?
salt/states/file.py
Outdated
contents += line.rstrip('\n').rstrip('\r') + os.linesep | ||
if contents_newline and not contents.endswith(os.linesep): | ||
contents += os.linesep | ||
if not line == validated_contents[-1]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some logical error here. Probably correct one is:
if not line == part[-1]:
But it would be also wrong to compare the contents because the content could contains equal data, say
line
line
line
So you probably will want something like this:
lines = part.splitlines()
for num, line in enumerate(lines):
if not num == len(lines) - 1
…to contents_newline_new_fix
@waynew @DmitryKuzmenko Are we good here? |
@dwoz @DmitryKuzmenko which release will include this particular fix? TIA! |
@xeacott what is the nearest planned salt release with this fix included? TIA! |
@defanator This fix will be part of the upcoming Salt 3000 (Neon) release. |
What does this PR do?
Address the issue of the file.managed state not using contents_newline correctly.
What issues does this PR fix or reference?
Issue #54177
Previous Behavior
Because of the for loop EOF would contain a platform-specific line-ending, even if the user set
contents_newline = False. According to the documentation, contents_newline should allow the user
to specify that they don't want the file to end with a newline.
New Behavior
Setting
contents_newline = False
will now determine if the last entry in the file should end witha newline character
Tests written?
Yes
Commits signed with GPG?
Yes