Skip to content

Commit 4a5c160

Browse files
committed
Assert the files the result cache e2e tests re-analyse with result-cache-info
The tests read "Result cache restored. N files will be reanalysed." from the verbose output of analyse. result-cache-info --json runs the same restore() before the analysis and tells whether the result cache is used and how many files would be analysed, which jq reads. The analyse runs stay where later steps need the cache they save or check their errors. Each converted assertion fails when the fix it protects is reverted, except a few counts that are only preconditions of their test (the worker count, collected data rule errors, the atomic save), where the old assertion did not catch it either. editor-mode keeps its assertion: result-cache-info has no --tmp-file and --instead-of. On Windows, jq.exe ends its output with CRLF, so the Windows tests read it with --join-output. Amp-Thread-ID: https://ampcode.com/threads/T-01a1c5c8-8d5b-7459-a22c-beb8e7ba8a3b
1 parent cff3173 commit 4a5c160

21 files changed

Lines changed: 243 additions & 104 deletions

File tree

‎e2e/bug-15125/bug-15125_test.sh‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ function test_bug_15125() {
1818
# "..", so a second run without any change must still restore the whole cache without
1919
# reporting a metadata mismatch.
2020
../../bin/phpstan analyse
21+
INFO=$(../../bin/phpstan result-cache-info --json)
22+
echo "$INFO"
23+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
24+
assert_equals '0' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
25+
# The Composer metadata must not be reported as changed either, which -vvv shows.
2126
OUTPUT=$(../../bin/phpstan analyse -vvv 2>&1)
2227
echo "$OUTPUT"
23-
assert_not_contains 'Result cache not used because the metadata do not match' "$OUTPUT"
2428
assert_not_contains 'Composer metadata changed but no package versions changed' "$OUTPUT"
25-
assert_contains 'Result cache restored. 0 files will be reanalysed.' "$OUTPUT"
2629
# --autoload-file is recorded in executedFilesHashes under the path it was given with;
2730
# a './' segment must not keep the cache from being reused either.
2831
../../bin/phpstan analyse -a ./vendor/autoload.php
29-
OUTPUT=$(../../bin/phpstan analyse -a ./vendor/autoload.php -vvv 2>&1)
30-
echo "$OUTPUT"
31-
assert_not_contains 'Result cache not used because the metadata do not match' "$OUTPUT"
32-
assert_contains 'Result cache restored. 0 files will be reanalysed.' "$OUTPUT"
32+
INFO=$(../../bin/phpstan result-cache-info -a ./vendor/autoload.php --json)
33+
echo "$INFO"
34+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
35+
assert_equals '0' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
3336
}

‎e2e/ext-ds-version/ext-ds-version_test.sh‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,34 @@ function tear_down() {
1212
function test_changed_ext_ds_version_reanalyses_dependent_files() {
1313
cp composer-json/v1-platform.json composer.json
1414
composer install --ignore-platform-reqs
15-
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
15+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
1616
assert_same "1" "$CODE"
1717
echo "$OUTPUT"
1818
assert_contains 'Foo.php:12:Instantiated class Ds\Seq not found.' "$OUTPUT"
1919
# Only composer.json changes - composer.lock and installed.php stay as they are - and it now
2020
# selects the ext-ds 2 stubs. src/Bar.php has no error with ext-ds 1, so only its dependency on
2121
# ext-ds re-analyses it; src/Baz.php does not use ext-ds and stays cached.
2222
cp composer-json/v2-platform.json composer.json
23+
INFO=$(../../bin/phpstan result-cache-info --json)
24+
echo "$INFO"
25+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
26+
assert_equals '2' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
2327
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
2428
assert_same "1" "$CODE"
2529
echo "$OUTPUT"
2630
assert_contains 'PHP extension versions changed (ext-ds); re-analysing the files depending on them and the files with errors.' "$OUTPUT"
27-
assert_contains 'Result cache restored. 2 files will be reanalysed.' "$OUTPUT"
2831
assert_contains 'Foo.php:11:Instantiated class Ds\Vector not found.' "$OUTPUT"
2932
assert_contains 'Bar.php:15:Call to an undefined method Ds\Pair<string, int>::copy().' "$OUTPUT"
3033
assert_not_contains 'Ds\Seq' "$OUTPUT"
3134
cp composer-json/v1-platform.json composer.json
35+
INFO=$(../../bin/phpstan result-cache-info --json)
36+
echo "$INFO"
37+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
38+
assert_equals '2' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
3239
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
3340
assert_same "1" "$CODE"
3441
echo "$OUTPUT"
3542
assert_contains 'PHP extension versions changed (ext-ds); re-analysing the files depending on them and the files with errors.' "$OUTPUT"
36-
assert_contains 'Result cache restored. 2 files will be reanalysed.' "$OUTPUT"
3743
assert_contains 'Foo.php:12:Instantiated class Ds\Seq not found.' "$OUTPUT"
3844
assert_not_contains 'Ds\Vector' "$OUTPUT"
3945
assert_not_contains 'Bar.php' "$OUTPUT"

‎e2e/result-cache-1/result-cache-1_test.sh‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ function test_result_cache_1() {
2525
function test_windows_plain_project() {
2626
echo -n > phpstan-baseline.neon
2727
php ../../bin/phpstan analyse --no-ansi
28-
php ../../bin/phpstan analyse -vv --no-ansi 2>&1 | tee output.txt
29-
grep -F "Result cache restored. 0 files will be reanalysed." output.txt && CODE=0 || CODE=$?
30-
assert_same "0" "$CODE"
28+
INFO=$(php ../../bin/phpstan result-cache-info --json --no-ansi)
29+
echo "$INFO"
30+
# jq.exe ends its output with CRLF, and the command substitution would keep the CR:
31+
# --join-output prints no line ending at all.
32+
assert_equals 'true' "$(echo "$INFO" | jq --join-output '.resultCacheUsed')"
33+
assert_equals '0' "$(echo "$INFO" | jq --join-output '.filesToAnalyseCount')"
3134
}

‎e2e/result-cache-atomic-save/result-cache-atomic-save_test.sh‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ function test_result_cache_atomic_save() {
2828
# A completed save leaves nothing next to the cache file.
2929
LEFTOVERS=$(ls tmp/ | grep -c '\.tmp$' || true)
3030
assert_equals '0' "$LEFTOVERS"
31-
OUTPUT=$(../../bin/phpstan -vvv 2>&1)
32-
echo "$OUTPUT"
33-
assert_contains 'Result cache restored. 0 files will be reanalysed.' "$OUTPUT"
31+
INFO=$(../../bin/phpstan result-cache-info --json)
32+
echo "$INFO"
33+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
34+
assert_equals '0' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
3435
}

‎e2e/result-cache-autoloaded-constant/result-cache-autoloaded-constant_test.sh‎

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,73 @@ function test_result_cache_autoloaded_constant() {
1414
# The same sequence on a project file that is neither analysed nor scanned - it is only
1515
# reachable through the Composer autoloader, so it is tracked through the dependency graph
1616
# alone.
17-
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
17+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
1818
assert_same "1" "$CODE"
1919
echo "$OUTPUT"
2020
assert_contains 'Foo.php:10:Dumped type: 1' "$OUTPUT"
2121
# The constant is renamed to one the class already declares. The file is invalid PHP, but
2222
# the class still reflects, so the file using the constant is re-analysed through its
2323
# dependency edge.
2424
patch -b other/Dep.php < duplicate.patch
25-
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
25+
INFO=$(../../bin/phpstan result-cache-info --json)
26+
echo "$INFO"
27+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
28+
assert_equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
29+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
2630
assert_same "1" "$CODE"
2731
echo "$OUTPUT"
28-
assert_contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT"
2932
assert_contains 'Foo.php:10:Access to undefined constant ResultCacheE2EAutoloadedConstant\Dep::CON.' "$OUTPUT"
3033
# Renamed to something else instead.
3134
mv other/Dep.php.orig other/Dep.php
3235
patch -b other/Dep.php < renamed.patch
33-
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
36+
INFO=$(../../bin/phpstan result-cache-info --json)
37+
echo "$INFO"
38+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
39+
assert_equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
40+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
3441
assert_same "1" "$CODE"
3542
echo "$OUTPUT"
36-
assert_contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT"
3743
assert_contains 'Foo.php:10:Access to undefined constant ResultCacheE2EAutoloadedConstant\Dep::CON.' "$OUTPUT"
3844
# Broken the same way again.
3945
mv other/Dep.php.orig other/Dep.php
4046
patch -b other/Dep.php < duplicate.patch
41-
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
47+
INFO=$(../../bin/phpstan result-cache-info --json)
48+
echo "$INFO"
49+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
50+
assert_equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
51+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
4252
assert_same "1" "$CODE"
4353
echo "$OUTPUT"
44-
assert_contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT"
4554
assert_contains 'Foo.php:10:Access to undefined constant ResultCacheE2EAutoloadedConstant\Dep::CON.' "$OUTPUT"
4655
# And put back the way it was.
4756
mv other/Dep.php.orig other/Dep.php
48-
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
57+
INFO=$(../../bin/phpstan result-cache-info --json)
58+
echo "$INFO"
59+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
60+
assert_equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
61+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
4962
assert_same "1" "$CODE"
5063
echo "$OUTPUT"
51-
assert_contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT"
5264
assert_contains 'Foo.php:10:Dumped type: 1' "$OUTPUT"
5365
# A syntax error makes the class disappear altogether: the file using it reports an unknown
5466
# class and records no dependency edge to the file, so nothing but the file being fixed can
5567
# bring the file using it back - it must be re-analysed once the class is there again.
5668
patch -b other/Dep.php < broken.patch
57-
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
69+
INFO=$(../../bin/phpstan result-cache-info --json)
70+
echo "$INFO"
71+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
72+
assert_equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
73+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
5874
assert_same "1" "$CODE"
5975
echo "$OUTPUT"
60-
assert_contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT"
6176
assert_contains 'Foo.php:10:Access to constant CON on an unknown class ResultCacheE2EAutoloadedConstant\Dep.' "$OUTPUT"
6277
mv other/Dep.php.orig other/Dep.php
63-
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
78+
INFO=$(../../bin/phpstan result-cache-info --json)
79+
echo "$INFO"
80+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
81+
assert_equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
82+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
6483
assert_same "1" "$CODE"
6584
echo "$OUTPUT"
66-
assert_contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT"
6785
assert_contains 'Foo.php:10:Dumped type: 1' "$OUTPUT"
6886
}

‎e2e/result-cache-autoloaded/result-cache-autoloaded_test.sh‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ function test_result_cache_autoloaded() {
1616
# through the Composer autoloader - is tracked as a file dependency too, so editing it
1717
# re-analyses the files depending on it instead of leaving stale results behind.
1818
patch -b other/Dep.php < patch.patch
19-
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
19+
INFO=$(../../bin/phpstan result-cache-info --json)
20+
echo "$INFO"
21+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
22+
assert_equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
23+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
2024
assert_same "1" "$CODE"
2125
echo "$OUTPUT"
22-
assert_contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT"
2326
assert_contains 'Foo.php:12:Method ResultCacheE2EAutoloaded\Foo::doFoo() should return int but returns string.' "$OUTPUT"
2427
}

‎e2e/result-cache-collected-data-rules/result-cache-collected-data-rules_test.sh‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ function test_result_cache_collected_data_rules() {
2929
assert_not_contains 'Result cache reused the errors' "$OUTPUT"
3030
# src/Other.php is analysed again, but what it collects stays the same.
3131
echo '// changed' >> src/Other.php
32+
INFO=$(../../bin/phpstan result-cache-info --json)
33+
echo "$INFO"
34+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
35+
assert_equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
3236
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
3337
assert_same "1" "$CODE"
3438
echo "$OUTPUT"
35-
assert_contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT"
3639
assert_contains 'Caller.php:10:Call to function ResultCacheCollectedDataRules\pure() on a separate line has no effect.' "$OUTPUT"
3740
echo "$OUTPUT" | grep -E 'Result cache reused the errors of ([0-9]+) of \1 rules on collected data\.'
3841
# Removing the call changes what src/Caller.php collects, so the rule runs again and the error goes away.

‎e2e/result-cache-composer-lock/result-cache-composer-lock_test.sh‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,25 @@ function test_result_cache_composer_lock() {
1717
# when composer.lock is not committed. The result cache must be kept, not discarded.
1818
jq '.["content-hash"]="0000000000000000000000000000000000000000"' composer.lock > composer.lock.new
1919
mv composer.lock.new composer.lock
20+
INFO=$(../../bin/phpstan result-cache-info --json)
21+
echo "$INFO"
22+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
23+
assert_equals '0' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
2024
OUTPUT=$(../../bin/phpstan analyse -vv 2>&1) && CODE=0 || CODE=$?
2125
assert_same "0" "$CODE"
2226
echo "$OUTPUT"
2327
assert_contains 'Composer metadata changed but no package versions changed; keeping the result cache.' "$OUTPUT"
24-
assert_contains 'Result cache restored. 0 files will be reanalysed.' "$OUTPUT"
2528
# Change the content hash again AND edit a single source file: the cache is still kept
2629
# (no package changed), and only the one changed file is reanalysed.
2730
printf '\n' >> src/Foo.php
2831
jq '.["content-hash"]="1111111111111111111111111111111111111111"' composer.lock > composer.lock.new
2932
mv composer.lock.new composer.lock
33+
INFO=$(../../bin/phpstan result-cache-info --json)
34+
echo "$INFO"
35+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
36+
assert_equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
3037
OUTPUT=$(../../bin/phpstan analyse -vv 2>&1) && CODE=0 || CODE=$?
3138
assert_same "0" "$CODE"
3239
echo "$OUTPUT"
3340
assert_contains 'Composer metadata changed but no package versions changed; keeping the result cache.' "$OUTPUT"
34-
assert_contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT"
3541
}

‎e2e/result-cache-extension-attribute-services/result-cache-extension-attribute-services_test.sh‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,26 @@ function test_result_cache_extension_attribute_services() {
1313
composer install
1414
# The rule is registered through the attributeServicesDirectories section, not in services:.
1515
# Editing it has to invalidate the cache, editing another class in the same directory does not.
16-
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
16+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
1717
assert_same "1" "$CODE"
1818
echo "$OUTPUT"
1919
assert_contains 'the rule says one thing' "$OUTPUT"
2020
patch -p0 < foo.patch
21-
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
21+
INFO=$(../../bin/phpstan result-cache-info --json)
22+
echo "$INFO"
23+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
24+
assert_equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
25+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
2226
assert_same "1" "$CODE"
2327
echo "$OUTPUT"
24-
assert_contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT"
2528
patch -p0 < rule.patch
26-
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
29+
INFO=$(../../bin/phpstan result-cache-info --json)
30+
echo "$INFO"
31+
assert_equals 'false' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
32+
assert_contains 'Result cache not used because extension file' "$(echo "$INFO" | jq --raw-output '.notUsedReason')"
33+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
2734
assert_same "1" "$CODE"
2835
echo "$OUTPUT"
29-
assert_contains 'Result cache not used because extension file' "$OUTPUT"
3036
assert_contains 'the rule says another thing' "$OUTPUT"
3137
assert_not_contains 'the rule says one thing' "$OUTPUT"
3238
}

‎e2e/result-cache-moved-tmpdir/result-cache-moved-tmpdir_test.sh‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function test_result_cache_moved_tmpdir() {
1515
# usable: same project, same analysis, cache read from a different tmpDir.
1616
mkdir -p tmp-b
1717
cp tmp-a/resultCache.php tmp-b/resultCache.php
18-
OUTPUT=$(../../bin/phpstan analyse -c moved.neon -vv 2>&1)
19-
echo "$OUTPUT"
20-
assert_contains 'Result cache restored. 0 files will be reanalysed.' "$OUTPUT"
21-
assert_not_contains 'metadata do not match' "$OUTPUT"
18+
INFO=$(../../bin/phpstan result-cache-info -c moved.neon --json)
19+
echo "$INFO"
20+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
21+
assert_equals '0' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
2222
}

0 commit comments

Comments
 (0)