kenjis / ci-app-for-ci-phpunit-test

CodeIgniter Test Application for ci-phpunit-test
31 stars 20 forks source link

Ignore write cache file exception for root user(also for Windows). #7

Closed tianhe1986 closed 7 years ago

tianhe1986 commented 8 years ago

I get the following messages while running unittest with root user.

There were 2 failures:

1) CIPHPUnitTestFileCache_test::test_construct_fail_to_create_cache_file
Failed asserting that exception of type "RuntimeException" is thrown.

/root/testgit/ci-app-for-ci-phpunit-test/vendor/phpunit/phpunit/phpunit:47

2) CIPHPUnitTestFileCache_test::test_construct_fail_to_create_dir
Failed asserting that exception of type "RuntimeException" is thrown.

/root/testgit/ci-app-for-ci-phpunit-test/vendor/phpunit/phpunit/phpunit:47

I tried to use "is_really_writable" at the beginning for checking(see here) but it also failed.

I got the same result even with the following code, "is_writable" returns false with root user when running with phpunit.

        public function test_construct_fail_to_create_cache_file()
        {
                chmod(__DIR__.'/tmp', 0);
                clearstatcache();
                if(is_writable(__DIR__.'/tmp'))
                {
                        $this->markTestSkipped('Ignored for root user');
                }
                $cache_file = __DIR__.'/tmp/cache_fail.php';
                @new CIPHPUnitTestFileCache($cache_file);
        }

However, it works well with the following code put in a single file being executed with php command-line.

<?php
function test_construct_fail_to_create_cache_file()
{
        if( ! file_exists(__DIR__.'/tmp'))
        {
                mkdir(__DIR__.'/tmp', 0777, true);
        }
        chmod(__DIR__.'/tmp', 0777);
        clearstatcache();
        var_dump(is_writable(__DIR__.'/tmp'));
        chmod(__DIR__.'/tmp', 0);
        clearstatcache();
        var_dump(is_writable(__DIR__.'/tmp'));
}
test_construct_fail_to_create_cache_file();

The output is

bool(true)
bool(false)

with non-root user , and is

bool(true)
bool(true)

with root user.

Could you help to test it in case of my wrong configuration ?

Signed-off-by: tianhe1986 w1s2j3229@163.com

tianhe1986 commented 8 years ago

@kenjis

I read your code and find you use IncludeStream as stream_wrapper, which caused is_writable problem.

Then I find deep into php source code and consider it to be a bug, and I report it(see here)

However, this pull request may still be right.

kenjis commented 8 years ago

@tianhe1986 I have finally got a Windows 10 for testing. I would like to test it later.

tianhe1986 commented 8 years ago

@kenjis Thank you. You can also use linux with root user for testing.

kenjis commented 8 years ago

I also got the following failures on Windows 10.

There were 2 failures:

1) CIPHPUnitTestFileCache_test::test_construct_fail_to_create_cache_file
Failed asserting that exception of type "RuntimeException" is thrown.

2) CIPHPUnitTestFileCache_test::test_construct_fail_to_create_dir
Failed asserting that exception of type "RuntimeException" is thrown.
kenjis commented 8 years ago

@tianhe1986 I run your PR code on Windows 10, but I got an error:

A PHP Error was encountered

Severity:    Warning
Message:     rmdir(C:\...\ci-app-for-ci-phpunit-test\application\tests\_tests/tmp): Directory not empty
Filename:    C:\Users\Kenji\work\ci-app-for-ci-phpunit-test\vendor\kenjis\ci-phpunit-test\application\tests\_ci_phpunit_test\patcher\IncludeStream.php
Line Number: 196

The test results:

There was 1 failure:

1) CIPHPUnitTestFileCache_test::test_construct_fail_to_create_dir
Failed asserting that exception of type "RuntimeException" is thrown.

--

There was 1 skipped test:

1) CIPHPUnitTestFileCache_test::test_construct_fail_to_create_cache_file
Ignored for root user

On Windows, one test still fails.

tianhe1986 commented 8 years ago

@kenjis Maybe you need to delete "C:...\ci-app-for-ci-phpunit-test\application\tests_tests/tmp" dir before running test.

PS: What about running test with your original master branch on windows?

kenjis commented 8 years ago

@tianhe1986

PS: What about running test with your original master branch on windows?

2 failures. See https://github.com/kenjis/ci-app-for-ci-phpunit-test/pull/7#issuecomment-234712255

tianhe1986 commented 8 years ago

@kenjis I see.

I think you run your master branch test and after then my PR without delete the "tmp" directory and also its sub directory , right ? Then mkdir function would return false since file already exists.

I should also modify the "tearDownAfterClass" code to clear directory recursively, but I'm a little lazy.

So could you run my PR code agian after delete the "tmp" directory manually? It may be the source of this problem.

kenjis commented 7 years ago

@tianhe1986 You're right. I merged this PR.