Skip to content

Conversation

@alzwded
Copy link
Contributor

@alzwded alzwded commented Nov 4, 2025

Description

In tests_file.lua, mock vim.fn.executable to yield 1 or 0 depending on what the test is trying to test.

Related Issue(s)

Screenshots

Checklist

  • I've read the contributing guidelines and have adhered to them in this PR
  • I've added test coverage for this fix/feature
  • I've run make all to ensure docs are generated, tests pass and my formatting is applied
  • (optional) I've updated CodeCompanion.has in the init.lua file for my new feature
  • (optional) I've updated the README and/or relevant docs pages

@olimorris olimorris added the P4 Negligible impact and urgency label Nov 4, 2025
@alzwded alzwded changed the title Fixes #2342 Fixes #2342 -- get_mimetype tests fail without file in PATH Nov 4, 2025
@olimorris
Copy link
Owner

It's early here but I don't understand this test

@alzwded
Copy link
Contributor Author

alzwded commented Nov 4, 2025

I've only learnt about these two tests 30 minutes ago when I rebased to main :-P

Initially, I wanted to add MiniTest.skip, but then I realized it wasn't actually executing anything.

Going off the testcase descriptions, one tests that get_mimetype invokes the file executable with those arguments, and the other checks that if the file executable is not available, then it is supposed to return a mime type using a fallback-extension based-lookup table.

T["Files utils"]["get_mimetype"]["can invoke `file`"]
T["Files utils"]["get_mimetype"]["works without `file`"]

That's not what the tests were doing, though. This PR adds a mock for vim.fn.executable so that the test setup matches the testcase description.

The implementation in utils.files first checks if there is a file in path, and only calls vim.system if there is one.

I don't particularly care if you accept this PR, but the docs say file is optional, the implementation has a fallback, and the tests are failing if there is no file in PATH (which happens to be the default state on Windows)

@Davidyz
Copy link
Contributor

Davidyz commented Nov 5, 2025

@olimorris the new get_mimetype uses the file command to detect mimetype when it's available. The vim.system is mocked because I've had some weird experiences with path handling on GitHub Actions (tl;dr is there seems to be some symlinks that make paths weird and may break the tests for no reason).

Regarding this PR, I suggest the following changes:

  1. In T["Files utils"]["get_mimetype"]["can invoke file"], we mock vim.fn.executable so that it returns 1 and triggers the (mocked) file command. This test makes sure the get_mimetype function can parse the output of the file command.
  2. Rename T["Files utils"]["get_mimetype"]["works without file"] to T["Files utils"]["get_mimetype"]["works with fallback"], and mock vim.fn.executable to return 0 so that it never invokes the file command. This test makes sure the hardcoded map is working.

1. In T["Files utils"]["get_mimetype"]["can invoke file"], we mock vim.fn.executable so that it returns 1 and triggers the (mocked) file command. This test makes sure the get_mimetype function can parse the output of the file command.
2. Rename T["Files utils"]["get_mimetype"]["works without file"] to T["Files utils"]["get_mimetype"]["works with fallback"], and mock vim.fn.executable to return 0 so that it never invokes the file command. This test makes sure the hardcoded map is working.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P4 Negligible impact and urgency

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: get_mimetype tests fail without file in PATH

3 participants