Skip to content
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

Merged
merged 11 commits into from
Dec 9, 2019

Conversation

xeacott
Copy link
Contributor

@xeacott xeacott commented Oct 8, 2019

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

            for part in validated_contents:
                for line in part.splitlines():
                    contents += line.rstrip('\n').rstrip('\r') + os.linesep
            if contents_newline and not contents.endswith(os.linesep):
                contents += os.linesep

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 with
a newline character

Tests written?

Yes

Commits signed with GPG?

Yes

@xeacott xeacott requested a review from a team as a code owner October 23, 2019 16:13
@ghost ghost requested a review from DmitryKuzmenko October 23, 2019 16:13
# 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_:
Copy link
Contributor

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? 🤔

Copy link
Contributor Author

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

Copy link
Contributor Author

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?

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]:
Copy link
Contributor

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

@xeacott xeacott changed the title Allow windows to handle binary file in file.managed Address file.managed with contents_newline flag Nov 15, 2019
@dwoz
Copy link
Contributor

dwoz commented Dec 6, 2019

@waynew @DmitryKuzmenko Are we good here?

@dwoz dwoz merged commit 831d229 into saltstack:master Dec 9, 2019
@defanator
Copy link
Contributor

@dwoz @DmitryKuzmenko which release will include this particular fix? TIA!

@defanator
Copy link
Contributor

@xeacott what is the nearest planned salt release with this fix included? TIA!

@oeuftete
Copy link
Contributor

oeuftete commented Feb 7, 2020

@defanator This fix will be part of the upcoming Salt 3000 (Neon) release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants