Skip to content

Commit

Permalink
chore: throw exception on failure to locate endpoint artifact (#2746)
Browse files Browse the repository at this point in the history
  • Loading branch information
stobrien89 authored Aug 1, 2023
1 parent 014d8ee commit 2bcc32d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changes/nextrelease/endpoints-tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "enhancement",
"category": "EndpointV2",
"description": "Adds terminal failure if endpoints file cannot be found"
}
]
18 changes: 11 additions & 7 deletions src/EndpointV2/EndpointDefinitionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
*/
class EndpointDefinitionProvider
{
public static function getEndpointRuleset($service, $apiVersion)
public static function getEndpointRuleset($service, $apiVersion, $baseDir = null)
{
return self::getData($service, $apiVersion, 'ruleset');
return self::getData($service, $apiVersion, 'ruleset', $baseDir);
}

public static function getEndpointTests($service, $apiVersion)
public static function getEndpointTests($service, $apiVersion, $baseDir = null)
{
return self::getData($service, $apiVersion, 'tests');
return self::getData($service, $apiVersion, 'tests', $baseDir);
}

public static function getPartitions()
Expand All @@ -30,9 +30,9 @@ public static function getPartitions()
}
}

private static function getData($service, $apiVersion, $type)
private static function getData($service, $apiVersion, $type, $baseDir)
{
$basePath = __DIR__ . '/../data';
$basePath = $baseDir ? $baseDir : __DIR__ . '/../data';
$serviceDir = $basePath . "/{$service}";
if (!is_dir($serviceDir)) {
throw new \InvalidArgumentException(
Expand All @@ -54,8 +54,12 @@ private static function getData($service, $apiVersion, $type)

if (file_exists($rulesetPath . $fileName . '.json.php')) {
return require($rulesetPath . $fileName . '.json.php');
} else {
} elseif (file_exists($rulesetPath . $fileName . '.json')) {
return json_decode(file_get_contents($rulesetPath . $fileName . '.json'), true);
} else {
throw new \InvalidArgumentException(
'Specified ' . $type . ' endpoint file for ' . $service . ' with api version ' . $apiVersion . ' does not exist.'
);
}
}

Expand Down
29 changes: 29 additions & 0 deletions tests/EndpointV2/EndpointDefinitionProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,33 @@ public function testThrowsExceptionOnInvalidApiVersion()
$this->expectExceptionMessage('Invalid api version.');
EndpointDefinitionProvider::getEndpointRuleset('s3', '10-22-2022');
}

public function getEndpointFileProvider()
{
return [
['Ruleset'],
['Tests']
];
}

/**
* @dataProvider getEndpointFileProvider
*
* @param $type
*/
public function testThrowsExceptionOnMissingFiles($type)
{
$method = 'getEndpoint' . $type;
$type = strtolower($type);
$tmpdir = sys_get_temp_dir();
if (!is_dir($tmpdir . '/data/foo-service/08-05-1989/')) {
mkdir($tmpdir . '/data/foo-service/08-05-1989/', 0777, true);
}
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage("Specified {$type} endpoint file for foo-service with api version 08-05-1989 does not exist.");
EndpointDefinitionProvider::$method('foo-service', '08-05-1989', $tmpdir . '/data');
rmdir($tmpdir . 'data/' . 's3/' . '/08-05-1989');
rmdir($tmpdir . 'data/' . 's3/');
rmdir($tmpdir . 'data');
}
}

0 comments on commit 2bcc32d

Please sign in to comment.