Skip to content
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

make test directory configurable #283

Merged
merged 2 commits into from
May 5, 2021
Merged

make test directory configurable #283

merged 2 commits into from
May 5, 2021

Conversation

faustbrian
Copy link
Contributor

@faustbrian faustbrian commented Mar 29, 2021

Q A
Bug fix? no
New feature? yes
Fixed tickets #194

This is an implementation to make the test directory configurable as requested in #194.

It does so by introducing a new --test-directory argument. If this flag is set you can call Pest\testDirectory which will return the path value and optionally concatenate it with a filename. Calling testDirectory('directory/File.php') will return tests/directory/File.php by default.

// When using an invalid directory
❯ bin/pest --test-directory=blarbs

   Pest\Exceptions\InvalidUsesPath

  The path `/Users/brian/Work/fork/blarbs` is not valid.

Copy link
Member

@nunomaduro nunomaduro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Just wondering if this could be done at Plugin level? https://github.com/pestphp/pest-plugin-coverage/blob/50260c8671377a5082a06b9e228a984c13ed199c/src/Plugin.php#L76

@faustbrian
Copy link
Contributor Author

I tried that initially but there was one place that would result in issues because the custom path would be available too late, don't remember right now which. I'll take another look tomorrow at doing it that way.

@faustbrian
Copy link
Contributor Author

Just had a quick look and I think it was this line https://github.com/pestphp/pest/blob/master/src/Plugin.php#L25 that is causing issues with doing it inside of plugins because plugins require this method but this method already needs to know about the path before any plugins do so it'll fail at that point.

@nunomaduro
Copy link
Member

@faustbrian This is ready to be merged?

@faustbrian
Copy link
Contributor Author

Should be if you have no further requests for changes.

Copy link
Member

@nunomaduro nunomaduro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@olivernybroe Plugin / IDE wise, any changes required?

src/Pest.php Outdated

function testDirectory(string $file = ''): string
{
return $_ENV['PEST_TEST_DIRECTORY'] . $file;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace this by $_SERVER.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just this occurrence or all $_ENV usages in this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you actually use the TestSuite::getInstance()->setTestDirectory() instead of using ENVs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me test but if I remember right it wasn't possible because the test path had to be set before the instance was created.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that doesn't work because https://github.com/pestphp/pest/blob/master/src/TestSuite.php#L98-L100 ends up executing some things that already need access to the test path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative could be this change.

$testSuite = TestSuite::getInstance($rootPath, (new ArgvInput())->getParameterOption('--test-directory', 'tests'));

public static function getInstance(string $rootPath = null, string $testPath = null): TestSuite
{
    if (is_string($rootPath)) {
        self::$instance = new TestSuite($rootPath);

        if (is_string($testPath)) {
            self::$instance->testPath = $testPath;
        }

        foreach (Plugin::$callables as $callable) {
            $callable();
        }

        return self::$instance;
    }

    if (self::$instance === null) {
        throw new InvalidPestCommand();
    }

    return self::$instance;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative https://github.com/faustbrian/pest/commit/c8a3e7e1f4747f838292b75e44be8ac33892f2ff but results in an odd issue I need to check if this is preferred.

❯ bin/pest --test-directory=tests
PHPUnit 9.5.3 by Sebastian Bergmann and contributors.

Unknown option "--test-directory"

@olivernybroe
Copy link
Member

Hmm, by a quick look I don't think it will have an impact on the plugin.
As far as I remember, we don't depend on the naming of the directory, but if it is marked as a test folder.

The only thing is that this is of course not configurable via the plugin, so if we want the user to be able to configure this, we need to add a new feature. (if they don't use the env parameter mode)

@faustbrian
Copy link
Contributor Author

Pushed the changes from #283 (comment) but will need to check tomorrow what causes the Unknown option "--test-directory" issue once it tries to bootstrap plugins after it has registered the path. Most likely PHPUnit not liking the parameter and it somehow being passed down to it.

@faustbrian
Copy link
Contributor Author

@nunomaduro one issue with this implementation which might be worth considering is that there are a few places that create a TestSuite instance outside of the CLI so they don't have access to the --test-directory flag while an environment variable would be accessible to them.

@nunomaduro
Copy link
Member

@faustbrian That's fine. Can you make a pull request to docs?

@faustbrian
Copy link
Contributor Author

Will submit a PR for documentation tomorrow. Just pushed a fix for the Unknown option parameter since PHPUnit can't handle that so we have to remove it before passing argv to it.

@faustbrian
Copy link
Contributor Author

Updated the PR description and submitted the docs PR https://github.com/pestphp/docs/pull/41/files

src/Pest.php Outdated Show resolved Hide resolved
Copy link
Member

@nunomaduro nunomaduro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@owenvoke Can you test this pull request locally?

@owenvoke
Copy link
Member

owenvoke commented May 5, 2021

This seems to be working well for me, I was having issues originally (but they were due to symlinking when requiring the package), and then further issues (but that was as my phpunit.xml testsuites weren't up to date).

I'd be happy if someone else could also test this just to be sure, but I think it should be fine. 👍🏻

@nunomaduro nunomaduro merged commit daa01ea into pestphp:master May 5, 2021
@adrolli adrolli mentioned this pull request Nov 2, 2022
9 tasks
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.

4 participants