Sitelet https://github.com/NanmiCoder/MediaCrawler/pull/983
Skip to content

test: search store/ in Python instead of shelling out to grep - #983

Open
ChrisDoufu wants to merge 1 commit into
NanmiCoder:mainfrom
ChrisDoufu:pr/tests-no-grep
Open

ChrisDoufu wants to merge 1 commit into
NanmiCoder:mainfrom
ChrisDoufu:pr/tests-no-grep

Conversation

@ChrisDoufu

Copy link
Copy Markdown

Context

tests/test_no_user_info.py has two tests that search the store/ directory: test_store_no_forbidden_dict_keys and test_store_no_creator_orm_imports. These tests run grep -rnE through subprocess.

Problem

On Windows, these two tests fail. The failure does not come from the code under test.

  • In PowerShell, there is no grep command. subprocess.run raises FileNotFoundError.
  • In Git Bash, grep is available. But Python decodes the output with the console encoding (GBK on Chinese Windows). The decode fails, out.stdout is None, and the test raises AttributeError.

Thus, on Windows, these tests cannot find forbidden keys in store/.

Change

Add a helper, _grep_store(pattern). The helper reads each store/**/*.py file as UTF-8 and searches each line with re. It gives each hit as path:line:text, as grep -rn does. The two tests use this helper. The patterns and the assertions do not change. store/ contains only .py files, so the helper searches the same files as grep -r.

Test

In PowerShell, on main:

  • Before this change, the two tests fail with FileNotFoundError.
  • After this change, all 11 tests in the file pass.

The helper also finds hits when they exist. In a branch that writes "user_id": in store/, the test fails and shows each line.

🤖 Generated with Claude Code

test_store_no_forbidden_dict_keys and test_store_no_creator_orm_imports ran
`grep -rnE` through subprocess. On Windows this fails: PowerShell has no grep
(FileNotFoundError), and under Git Bash the output cannot be decoded with the
default GBK console encoding (stdout comes back as None). A small helper now
scans store/**/*.py with re and reports hits in the same path:line:text form.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant