Merge pull request #9492 from appwrite/chore-add-invalid-ssr-site-test

Throw build error for adapter mismatch
This commit is contained in:
Matej Bačo 2025-03-12 13:10:27 +01:00 committed by GitHub
commit 75b3e78098
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 4 deletions

View file

@ -691,7 +691,7 @@ class Builds extends Action
throw new \Exception('Build size should be less than ' . number_format($buildSizeLimit / 1048576, 2) . ' MBs.');
}
if ($resource->getCollection() === 'sites' && empty($resource->getAttribute('adapter', ''))) {
if ($resource->getCollection() === 'sites') {
// TODO: Refactor with structured command in future, using utopia library (CLI)
$listFilesCommand = "cd /usr/local/build && cd " . \escapeshellarg($resource->getAttribute('outputDirectory', './')) . " && find . -name 'node_modules' -prune -o -type f -print";
$command = $executor->createCommand(
@ -712,9 +712,15 @@ class Builds extends Action
->addOption(new XStatic());
$detection = $detector->detect();
$resource->setAttribute('adapter', $detection->getName());
$resource->setAttribute('fallbackFile', $detection->getFallbackFile() ?? '');
$resource = $dbForProject->updateDocument('sites', $resource->getId(), $resource);
$adapter = $resource->getAttribute('adapter', '');
if (empty($adapter)) {
$resource->setAttribute('adapter', $detection->getName());
$resource->setAttribute('fallbackFile', $detection->getFallbackFile() ?? '');
$resource = $dbForProject->updateDocument('sites', $resource->getId(), $resource);
} elseif ($adapter === 'ssr' && $detection->getName() === 'static') {
throw new \Exception('Adapter mismatch. Detected: ' . $detection->getName() . ' does not match with the set adapter: ' . $adapter);
}
}
$executor->deleteRuntime($project->getId(), $deployment->getId(), '-build');

View file

@ -2217,4 +2217,41 @@ class SitesCustomServerTest extends Scope
$this->cleanupSite($siteId);
}
public function testInvalidSSRSource(): void
{
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'Astro SSR Site',
'framework' => 'astro',
'adapter' => 'ssr',
'buildRuntime' => 'node-22',
'outputDirectory' => './dist',
'buildCommand' => 'npm run build',
'installCommand' => 'npm install',
]);
$this->assertNotEmpty($siteId);
$site = $this->getSite($siteId);
$this->assertEquals(200, $site['headers']['status-code']);
$this->assertArrayHasKey('adapter', $site['body']);
$this->assertEquals('ssr', $site['body']['adapter']);
$deployment = $this->createDeployment($siteId, [
'code' => $this->packageSite('astro-static'),
'activate' => true
]);
$this->assertEquals(202, $deployment['headers']['status-code']);
$deploymentId = $deployment['body']['$id'];
$this->assertNotEmpty($deploymentId);
$this->assertEventually(function () use ($siteId, $deploymentId) {
$deployment = $this->getDeployment($siteId, $deploymentId);
$this->assertEquals('failed', $deployment['body']['status'], 'Deployment status is failed, deployment: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT));
}, 100000, 500);
$this->cleanupSite($siteId);
}
}