-
-
Notifications
You must be signed in to change notification settings - Fork 53
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
feature: cache flush safety & cache flush command #118
base: main
Are you sure you want to change the base?
Conversation
… deletion of non-cached files in custom directories
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.
This PR makes sense and the code looks good to me
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.
@chr15k Good Contribution! I really like the number of test you added for the feature.
I left some change requests and quite a few questions to make sure I understand everything the way you intended.
It would be incredible, if you could also extend the README to document the new command. That way everybody could imideatly see the command and use it.
Before this, Nuno was unsure about adding new commands. This however makes sense to me. The final decision lies with him.
I will mark this as a draft for now. Please mark it as "ready for review" once you looked at the feedback and implemented some of the changes.
|
||
try { | ||
if (is_string($directory) && ! is_dir($directory)) { | ||
throw new Exception('The specified cache directory does not exist.'); |
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.
If this is the only exception that can be thrown, I would prefer to remove the try-catch and instead directly have an early return and $output->writeline()
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.
good spot, it is the only exception in this case; I've removed it. Thanks.
} | ||
|
||
match (is_string($directory)) { | ||
true => Cache::create($directory, $prefix)->flush(), |
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.
This expects the $directory
to be absolute correct, right? I think everywhere else we use a relative path. Feel free to correct me on this.
Either way, you could maybe explain how the path needs to be passed in the configure method.
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.
yeh good point, perhaps we can ensure the custom directory is always inside {project_root}/.peck.cache/
?
So if you pass a custom dir, it's {project_root}/.peck.cache/{directory}
?
And the config method can state something like: 'The cache directory path (relative to project root)'
|
||
$commandTester = new CommandTester($command); | ||
|
||
$commandTester->execute([]); |
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.
I'm a bit unsure about this, as this "interacts with"/changes the files outside the tests directory.
But I guess it is the only way to test this.
$output = $commandTester->getDisplay(); | ||
|
||
expect(trim($output))->toContain('Clearing cache...'); | ||
expect(trim($output))->toContain('Cache successfully cleared!'); |
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.
Maybe we should also check if the files where deleted? Or do we leave that to the unit test of the Cache class?
$commandTester = new CommandTester($command); | ||
|
||
if (! is_dir('/tmp/.peck.cache')) { | ||
@mkdir('/tmp/.peck.cache'); |
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.
Shouldn't you check if this directory was created?
If it was not created there is a permission problem. I am unsure how the test behaves in that scenario, when we ignore the errors.
'--prefix' => 'pecker_', | ||
]); | ||
|
||
expect(count(glob('/tmp/peck_custom/*')))->toBe(2); |
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.
This would also pass if the --prefix was ignored and the default was used (as the number of files for both prefixes are the same). If possible also assert the file names.
What:
Description:
The issue is if you provide a custom cache directory, the flush method will attempt to delete everything from it, potentially even unrelated files. One solution would be to namespace the cache, but in this case I've kept it simple and just added a default prefix
peck_*
to the md5 cache filename.Note
The only potential issue with this approach is having stale pre-existing cache. So I've added a prefix option to the flush command.
To achieve the existing flush behaviour (clear all regardless of prefix), you can provide an empty string as the prefix option (
bin/peck cache:clear --prefix=
)Changes:
Usage: