Skip to content

Commit e685509

Browse files
authored
fix examples (#699)
Examples are broken for 1. all functions 2. any class in a namespace 3. any examples without code (.md only) This fixes all of it, and adds a test.
1 parent 2116ae0 commit e685509

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hh-apidoc-extensions/PageSections/Examples.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@
1818
final class Examples extends PageSection {
1919
<<__Override>>
2020
public function getMarkdown(): ?string {
21-
$path = LocalConfig::ROOT.'/api-examples/';
2221
if ($this->parent !== null) {
23-
$path .= 'class.'.$this->parent->getName().'/';
22+
$subdirectory = 'class.'.$this->parent->getName().'/';
2423
} else {
25-
$path .= 'function';
24+
$subdirectory = 'function.';
2625
}
27-
$path .= Str\replace(
28-
$this->definition->getName(),
29-
"\\",
30-
'.',
26+
$subdirectory .= $this->definition->getName();
27+
28+
$path = Str\format(
29+
'%s/api-examples/%s',
30+
LocalConfig::ROOT,
31+
Str\replace($subdirectory, '\\', '.'),
3132
);
3233

33-
$examples = Vec\concat(\glob($path.'/*.php'), \glob($path.'/*.php'))
34+
$examples = Vec\concat(\glob($path.'/*.md'), \glob($path.'/*.php'))
3435
|> Vec\map($$, $file ==> \pathinfo($file, \PATHINFO_FILENAME))
3536
|> keyset($$);
3637

tests/APIPagesTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
APIDefinitionType,
77
APINavData,
88
APIProduct,
9+
LocalConfig,
910
NavDataNode,
1011
URLBuilder,
1112
};
1213

13-
use namespace HH\Lib\Vec;
14+
use namespace HH\Lib\{Str, Vec};
1415
use function Facebook\FBExpect\expect;
1516
use type Facebook\HackTest\{DataProvider, TestGroup};
1617

@@ -177,4 +178,28 @@ public function getDoNotDocument(): vec<(string, APIDefinitionType)> {
177178
\sprintf('"%s" should not be documented', $name),
178179
);
179180
}
181+
182+
public function getPagesWithExamples(): vec<vec<string>> {
183+
$root = LocalConfig::ROOT.'/api-examples';
184+
$urls = keyset[];
185+
foreach (\glob($root.'/class.*/*') as $dir) {
186+
if (!\is_dir($dir)) {
187+
continue;
188+
}
189+
$urls[] = Str\replace($dir, $root.'/class.', 'class/');
190+
}
191+
foreach (\glob($root.'/function.*') as $dir) {
192+
$urls[] = Str\replace($dir, $root.'/function.', 'function/');
193+
}
194+
return Vec\map($urls, $url ==> vec['/hack/reference/'.$url.'/']);
195+
}
196+
197+
<<DataProvider('getPagesWithExamples')>>
198+
public async function testExamples(string $url): Awaitable<void> {
199+
list($response, $body) = await PageLoader::getPageAsync($url);
200+
expect($response->getStatusCode())
201+
->toBeSame(200, 'Examples provided for non-existent page %s.', $url);
202+
expect(Str\contains($body, '<a href="#examples">'))
203+
->toBeTrue('Missing examples at %s.', $url);
204+
}
180205
}

0 commit comments

Comments
 (0)