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

Fix assert_symlink_to for temp files in OSX #56

Conversation

julian-mateu
Copy link

Why?

The assert_symlink_to function uses _bats_file_readlinkf_macos for OSX, which prepends the physical directory to the target file, but the assertion in assert_symlink_to compares the resolved link to the provided sourcefile, which does not have this physical directory, so the assertion will fail in some cases where these two differ, e.g. in a temp directory:

realsource=/private/var/folders/mm/241yd_bn0b5095s4tgq_r84c0000gq/T/65-assert-10-assert_symlink_to.bats-3-8MdiJY/file
sourcefile=/var/folders/mm/241yd_bn0b5095s4tgq_r84c0000gq/T/65-assert-10-assert_symlink_to.bats-3-8MdiJY/file

What?

This commit fixes the issue by defining a realexpectedsource variable which results from calling _bats_file_readlinkf_macos with the sourcefile, and makes that comparison instead, so that realsource matches the realexpectedsource for paths with a physical directory:

realexpectedsource=/private/var/folders/mm/241yd_bn0b5095s4tgq_r84c0000gq/T/65-assert-10-assert_symlink_to.bats-3-8MdiJY/file

Testing

This was tested by adding a new unit test in the
65-assert-10-assert_symlink_to.bats file, which fails with the previous logic and passes with the fix.

src/file.bash Outdated Show resolved Hide resolved
test/65-assert-10-assert_symlink_to.bats Outdated Show resolved Hide resolved
Julian Mateu added 3 commits March 17, 2023 09:41
The `assert_symlink_to` function uses `_bats_file_readlinkf_macos` for OSX, which prepends the physical directory to the target file, but the assertion in `assert_symlink_to` compares the resolved link to the provided `sourcefile`, which does not have this physical directory, so the assertion will fail in some cases where these two differ, e.g. in a temp directory:
```bash
realsource=/private/var/folders/mm/241yd_bn0b5095s4tgq_r84c0000gq/T/65-assert-10-assert_symlink_to.bats-3-8MdiJY/file
sourcefile=/var/folders/mm/241yd_bn0b5095s4tgq_r84c0000gq/T/65-assert-10-assert_symlink_to.bats-3-8MdiJY/file
```

This commit fixes the issue by defining a `realexpectedsource` variable which results from calling `_bats_file_readlinkf_macos` with the `sourcefile`, and makes that comparison instead, so that `realsource` matches the `realexpectedsource` for paths with a physical directory:
```bash
realexpectedsource=/private/var/folders/mm/241yd_bn0b5095s4tgq_r84c0000gq/T/65-assert-10-assert_symlink_to.bats-3-8MdiJY/file
```

This was tested by adding a new unit test in the
`65-assert-10-assert_symlink_to.bats` file, which fails with the previous logic and passes with the fix.
@julian-mateu julian-mateu force-pushed the fix-assert_symlink_to-for-temp-files branch from bbbbb5f to 7d6add4 Compare March 17, 2023 09:42
@julian-mateu
Copy link
Author

Merging was blocked because of missing signature on previous commits. I had to rebase and sign them, and force push.

@martin-schulze-vireso martin-schulze-vireso merged commit cc3fb8c into bats-core:master Mar 17, 2023
@martin-schulze-vireso
Copy link
Member

Thanks for your contribution.

@julian-mateu julian-mateu deleted the fix-assert_symlink_to-for-temp-files branch March 21, 2023 16:15
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.

2 participants