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

Add is_windows() global function #6884

Merged
merged 4 commits into from
Nov 24, 2022

Conversation

paulbalandan
Copy link
Member

@paulbalandan paulbalandan commented Nov 19, 2022

Description

  • Adds is_windows() global function, for uniform detection of Windows platform
  • Deprecate CLI::isWindows(), as platform detection is not limited to console
  • Normalize platform detection

Related: #6882

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

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

I read conflicting options on whether uname() or if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') is better. If you feel confident this is the way then I trust you.

We should find a way to make this mockable so people can test their Windows-specific code without being on Windows. My initial thought is to add a parameter to set/clear a local static value:

public function testWindowsUsesBananas() {
    is_windows(true);

    $this->assertSame('bananas', $this->execute('getFruitByOs');

    is_windows(null);
}

It's a little bit hacky but thus is functional programming 🤷‍♂️ Only other alternative might be to stick the value on CodeIgniter along with $context.

@paulbalandan
Copy link
Member Author

paulbalandan commented Nov 19, 2022

On WSL, PHP_OS is Linux so the check would fail on that. DIRECTORY_SEPARATOR is also "/" on WSL.

@kenjis
Copy link
Member

kenjis commented Nov 20, 2022

De we really need to detect WSL as Windows?

It seems WSL supports Chmod/Chown.
https://devblogs.microsoft.com/commandline/chmod-chown-wsl-improvements/

@paulbalandan
Copy link
Member Author

It seems WSL supports Chmod/Chown. https://devblogs.microsoft.com/commandline/chmod-chown-wsl-improvements/

It seems the article is referring to the native filesystem functions, not the PHP equivalents. As tested in WSL, PHP's chmod behaves as if running on native Windows.

@kenjis
Copy link
Member

kenjis commented Nov 21, 2022

Do you mount your drive with -o metadata?
See microsoft/WSL#3172

In my understanding, PHP's chmod() is kind of wrapper for the native filesystem functions.

@paulbalandan
Copy link
Member Author

Thanks for the link!

I was not able to make it work on the first try. I first need to uninstall all php related from WSL, then proceeding with unmounting and remounting:

sudo apt remove -y php8.2-*
cd /
sudo umount /mnt/c
sudo mount -t drvfs C: /mnt/c -o metadata,uid=1000,gid=1000

Now, chmod works on WSL:

$ vendor/bin/phpunit --filter GeneratorsTest --no-coverage
PHPUnit 9.5.26 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.0RC5
Configuration: /mnt/c/Users/P/Desktop/Web Dev/CodeIgniter4/phpunit.xml.dist

........                                                                                                                              8 / 8 (100%)

Nexus\PHPUnit\Extension\Tachycardia identified these 4 slow tests:
⚠  Took 5.50s from 0.50s limit to run CodeIgniter\\Commands\\GeneratorsTest::testGenerateFileCreated
⚠  Took 1.52s from 0.50s limit to run CodeIgniter\\Commands\\GeneratorsTest::testSuffixingHasNoEffect
⚠  Took 1.36s from 0.50s limit to run CodeIgniter\\AutoReview\\FrameworkCodeTest::testEachTestClassHasCorrectGroupAnnotation with data set \"CodeIgniter\\Commands\\GeneratorsTest\"
⚠  Took 0.54s from 0.50s limit to run CodeIgniter\\Commands\\GeneratorsTest::testGenerateFileFailsOnUnwritableDirectory


Time: 00:10.806, Memory: 44.00 MB

OK (8 tests, 14 assertions)

@paulbalandan
Copy link
Member Author

I'll update the PR to limit the function on native Windows.

@kenjis kenjis added the stale Pull requests with conflicts label Nov 22, 2022
@kenjis
Copy link
Member

kenjis commented Nov 22, 2022

Please update the docs. Otherwise looks good.

@kenjis kenjis removed the stale Pull requests with conflicts label Nov 23, 2022
@kenjis kenjis added the enhancement PRs that improve existing functionalities label Nov 23, 2022
system/Common.php Outdated Show resolved Hide resolved
Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

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

Thanks @paulbalandan! Looks good.

@juanma386
Copy link

Thank you very much for the improvements, from Argentina

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.3 enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants