Skip to content

Commit

Permalink
wip - tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Jan 16, 2025
1 parent de68451 commit fa53b11
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 0 deletions.
163 changes: 163 additions & 0 deletions tests/CopyToBuildDirectoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

use Native\Laravel\NativeServiceProvider;
use Native\Electron\Commands\BuildCommand;
use Symfony\Component\Filesystem\Filesystem;
use Native\Electron\Traits\CopiesToBuildDirectory;

/*
|--------------------------------------------------------------------------
| Setup
|--------------------------------------------------------------------------
*/
$sourcePath = testsDir('_test_source_path');
$buildPath = testsDir('_test_build_path');

beforeEach(function () use ($sourcePath, $buildPath) {
$filesystem = new Filesystem;
$filesystem->mkdir($sourcePath);
$filesystem->remove($buildPath);
});

afterEach(function () use ($sourcePath, $buildPath) {
$filesystem = new Filesystem;
$filesystem->remove($sourcePath);
$filesystem->remove($buildPath);
});

/*
|--------------------------------------------------------------------------
| Mock Build command with anonymous class
|--------------------------------------------------------------------------
*/
$command = new class($sourcePath, $buildPath)
{
use CopiesToBuildDirectory;

const CLEANUP_EXCLUDE_FILES = BuildCommand::CLEANUP_EXCLUDE_FILES;

public function __construct(
public $sourcePath,
public $buildPath
) {}

protected function sourcePath(string $path = ''): string
{
return $this->sourcePath.$path;
}

protected function buildPath(): string
{
return $this->buildPath;
}
};

/*
|--------------------------------------------------------------------------
| Tests
|--------------------------------------------------------------------------
*/
it('will remove the build directory by default before copying', function () use ($buildPath, $command) {

createFiles("$buildPath/test.txt");

expect("$buildPath/test.txt")->toBeFile();

$command->copyToBuildDirectory();

expect("$buildPath/test.txt")->not->toBeFile();
});

it('copies included files and directories', function () use ($sourcePath, $buildPath, $command) {

createFiles([
"$sourcePath/do-not-delete.txt",
"$sourcePath/foo-bar/do-not-delete.json",
]);

$command->copyToBuildDirectory();

expect([
"$buildPath/do-not-delete.txt",
"$buildPath/foo-bar/do-not-delete.json",
])->each->toBeFile();
});

it('skips directories by path', function () use ($sourcePath, $buildPath, $command) {
createFiles("$sourcePath/foo-bar/do-not-delete.json");

config()->set('nativephp.cleanup_exclude_files', [
'foo-bar'
]);

$command->copyToBuildDirectory();

expect("$buildPath/foo-bar")->not->toBeDirectory();
});

it('skips files by path', function () use ($sourcePath, $buildPath, $command) {
createFiles("$sourcePath/foo-bar/delete-me.json");

config()->set('nativephp.cleanup_exclude_files', [
'foo-bar/delete-me.json'
]);

$command->copyToBuildDirectory();

expect("$buildPath/foo-bar")->toBeDirectory();
expect("$buildPath/foo-bar/delete-me.json")->not->toBeFile();
});

it('skips directories by wildcard path')->todo();
it('skips files by wildcard path')->todo();
it('can combine multiple files in a single glob')->todo();

it('will never include files that may contain sensitive information', function () use ($sourcePath, $buildPath, $command) {

$sourceFiles = createFiles([
"$sourcePath/do-not-delete.txt",
"$sourcePath/database/wildcard.sqlite",
"$sourcePath/database/wildcard.sqlite-shm",
"$sourcePath/database/wildcard.sqlite-wal",
"$sourcePath/storage/framework/sessions/wildcard.txt",
"$sourcePath/storage/framework/testing/wildcard.txt",
"$sourcePath/storage/framework/cache/wildcard.txt",
"$sourcePath/storage/framework/views/wildcard.txt",
"$sourcePath/storage/logs/wildcard.log",
]);

expect($sourceFiles)->each->toBeFile();

$command->copyToBuildDirectory();

expect([
"$buildPath/database/wildcard.sqlite",
"$buildPath/database/wildcard.sqlite-shm",
"$buildPath/database/wildcard.sqlite-wal",
"$buildPath/storage/framework/sessions/wildcard.txt",
"$buildPath/storage/framework/testing/wildcard.txt",
"$buildPath/storage/framework/cache/wildcard.txt",
"$buildPath/storage/framework/views/wildcard.txt",
"$buildPath/storage/logs/wildcard.log",
])->each->not->toBeFile();

expect("$buildPath/do-not-delete.txt")->toBeFile();
});

it('makes sure required folders are not empty', function () use ($buildPath, $command) {

$required = [
"{$buildPath}/storage/framework/cache/_native.json",
"{$buildPath}/storage/framework/sessions/_native.json",
"{$buildPath}/storage/framework/testing/_native.json",
"{$buildPath}/storage/framework/views/_native.json",
"{$buildPath}/storage/app/public/_native.json",
"{$buildPath}/storage/logs/_native.json",
];

expect($required)->each->not->toBeFile();

$command->copyToBuildDirectory();

expect($required)->each->toBeFile();
});
18 changes: 18 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
<?php

use Native\Electron\Tests\TestCase;
use Symfony\Component\Filesystem\Filesystem;

uses(TestCase::class)->in(__DIR__);

function testsDir(string $path = ''): string
{
return __DIR__.'/'.$path;
}

function createFiles(array|string $paths): array
{
$paths = (array) $paths;
$filesystem = new Filesystem;

foreach ($paths as $path) {
$filesystem->dumpFile($path, '');
}

return $paths;
}

0 comments on commit fa53b11

Please sign in to comment.