Skip to content

Commit 203cd83

Browse files
authored
[clang-doc] Add Mustache case to assets test (#170198)
Mustache wasn't tested in the assets lit test, which tests if user-supplied assets are copied correctly. The Mustache HTML backend initialy failed this test because it expected every asset, which included Mustache templates, to be supplied. For now, we just expect either CSS or JS to be supplied and use the default if one of them isn't given. We can allow custom templates in the future using the same checks.
1 parent 1d84c89 commit 203cd83

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

clang-tools-extra/clang-doc/support/Utils.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,20 @@ void getMustacheHtmlFiles(StringRef AssetsPath,
3333
assert(!AssetsPath.empty());
3434
assert(sys::fs::is_directory(AssetsPath));
3535

36-
SmallString<128> DefaultStylesheet =
37-
appendPathPosix(AssetsPath, "clang-doc-mustache.css");
36+
// TODO: Allow users to override default templates with their own. We would
37+
// similarly have to check if a template file already exists in CDCtx.
38+
if (CDCtx.UserStylesheets.empty()) {
39+
SmallString<128> DefaultStylesheet =
40+
appendPathPosix(AssetsPath, "clang-doc-mustache.css");
41+
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
42+
DefaultStylesheet.c_str());
43+
}
44+
45+
if (CDCtx.JsScripts.empty()) {
46+
SmallString<128> IndexJS = appendPathPosix(AssetsPath, "mustache-index.js");
47+
CDCtx.JsScripts.insert(CDCtx.JsScripts.begin(), IndexJS.c_str());
48+
}
49+
3850
SmallString<128> NamespaceTemplate =
3951
appendPathPosix(AssetsPath, "namespace-template.mustache");
4052
SmallString<128> ClassTemplate =
@@ -45,11 +57,7 @@ void getMustacheHtmlFiles(StringRef AssetsPath,
4557
appendPathPosix(AssetsPath, "function-template.mustache");
4658
SmallString<128> CommentTemplate =
4759
appendPathPosix(AssetsPath, "comment-template.mustache");
48-
SmallString<128> IndexJS = appendPathPosix(AssetsPath, "mustache-index.js");
4960

50-
CDCtx.JsScripts.insert(CDCtx.JsScripts.begin(), IndexJS.c_str());
51-
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
52-
DefaultStylesheet.c_str());
5361
CDCtx.MustacheTemplates.insert(
5462
{"namespace-template", NamespaceTemplate.c_str()});
5563
CDCtx.MustacheTemplates.insert({"class-template", ClassTemplate.c_str()});

clang-tools-extra/clang-doc/tool/ClangDocMain.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ static std::string getExecutablePath(const char *Argv0, void *MainAddr) {
151151
return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
152152
}
153153

154+
// TODO: Rename this, since it only gets custom CSS/JS
154155
static llvm::Error getAssetFiles(clang::doc::ClangDocContext &CDCtx) {
155156
using DirIt = llvm::sys::fs::directory_iterator;
156157
std::error_code FileErr;
@@ -221,8 +222,8 @@ static llvm::Error getMustacheHtmlFiles(const char *Argv0,
221222
llvm::outs() << "Asset path supply is not a directory: " << UserAssetPath
222223
<< " falling back to default\n";
223224
if (IsDir) {
224-
getMustacheHtmlFiles(UserAssetPath, CDCtx);
225-
return llvm::Error::success();
225+
if (auto Err = getAssetFiles(CDCtx))
226+
return Err;
226227
}
227228
void *MainAddr = (void *)(intptr_t)getExecutablePath;
228229
std::string ClangDocPath = getExecutablePath(Argv0, MainAddr);

clang-tools-extra/test/clang-doc/assets.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
// RUN: rm -rf %t && mkdir %t
22
// RUN: clang-doc --format=html --output=%t --asset=%S/Inputs/test-assets --executor=standalone %s --base base_dir
3+
// RUN: clang-doc --format=mustache --output=%t --asset=%S/Inputs/test-assets --executor=standalone %s --base base_dir
34
// RUN: FileCheck %s -input-file=%t/index.html -check-prefix=INDEX
45
// RUN: FileCheck %s -input-file=%t/test.css -check-prefix=CSS
56
// RUN: FileCheck %s -input-file=%t/test.js -check-prefix=JS
67

8+
// RUN: FileCheck %s -input-file=%t/html/test.css -check-prefix=CSS
9+
// RUN: FileCheck %s -input-file=%t/html/test.js -check-prefix=JS
10+
711
// INDEX: <!DOCTYPE html>
812
// INDEX-NEXT: <meta charset="utf-8"/>
913
// INDEX-NEXT: <title>Index</title>

0 commit comments

Comments
 (0)