Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
cf53a7c
[tests] JavaScript: extract searchindex.js-format test fixtures
jayaddison Mar 15, 2024
311c4f0
[tests] JavaScript: write Sphinx sources that produce acceptable sear…
jayaddison Mar 15, 2024
e0bee37
[tests] linting: add a ruff exclude filter for the JS fixture source …
jayaddison Mar 15, 2024
4340bbe
[tests] nitpick: reduce the changeset diff by retaining an index vari…
jayaddison Mar 16, 2024
40fc985
Add CHANGES.rst entry
jayaddison Mar 16, 2024
7e22d78
[tests] fixup: regenerate multiterm test search index
jayaddison Mar 16, 2024
8f753a8
[tests] refactor: use a separate index fixture directory per root
jayaddison Mar 16, 2024
dd1fde8
Merge branch 'master' into issue-12099-prep/extract-js-search-index-f…
jayaddison Mar 16, 2024
695037d
[search] Refactor search code to improve testability
jayaddison Mar 16, 2024
58b5e90
[tests] expect duplicate result due to #11961
jayaddison Mar 16, 2024
c37bb53
[tests] run 'js-beautify -r ...' on each searchindex.js file
jayaddison Mar 16, 2024
8ac1b6c
[tests] Fixup: regenerate cpp searchindex.js
jayaddison Mar 16, 2024
e31a7bd
Merge branch 'issue-12099-prep/extract-js-search-index-fixtures' into…
jayaddison Mar 16, 2024
fbaff49
[utils] Add script to regenerate JavaScript test fixtures
jayaddison Mar 16, 2024
d619a52
[tests] nitpick: relocate project-related JavaScript files away from …
jayaddison Mar 16, 2024
b33d331
Merge branch 'master' into issue-12099-prep/extract-js-search-index-f…
jayaddison Mar 18, 2024
55b6072
Merge branch 'issue-12099-utils/fixture-regeneration' into issue-1209…
jayaddison Mar 18, 2024
ee4316d
[tests] Regenerate test fixtures by running 'python utils/generate_js…
jayaddison Mar 18, 2024
0634502
Merge branch 'issue-12099-prep/extract-js-search-index-fixtures' into…
jayaddison Mar 18, 2024
0bc558a
[utils] Add script to regenerate JavaScript test fixtures
jayaddison Mar 16, 2024
a74bc15
[utils] Remove beautification step for generated searchindex.js files.
jayaddison Mar 22, 2024
a037867
Merge branch 'issue-12099-utils/fixture-regeneration' into refactor/e…
jayaddison Mar 22, 2024
68a6504
[tests] Regenerate searchindex.js files.
jayaddison Mar 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ doc/_build/
doc/locale/
tests/.coverage
tests/build/
tests/js/roots/*/build
tests/test-server.lock
utils/regression_test.js

Expand Down
1 change: 1 addition & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exclude = [
".tox",
".venv",
"tests/roots/*",
"tests/js/roots/*",
"build/*",
"doc/_build/*",
"sphinx/search/*",
Expand Down
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Testing

* pytest: report the result of ``test_run_epubcheck`` as ``skipped`` instead of
``success`` when Java and/or the ``epubcheck.jar`` code are not available.
* karma: refactor HTML search tests to use fixtures generated by Sphinx.

Release 7.2.6 (released Sep 13, 2023)
=====================================
Expand Down
6 changes: 4 additions & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ module.exports = function(config) {

// list of files / patterns to load in the browser
files: [
'tests/js/documentation_options.js',
'tests/js/project/documentation_options.js',
'tests/js/project/language_data.js',
'sphinx/themes/basic/static/doctools.js',
'sphinx/themes/basic/static/searchtools.js',
'sphinx/themes/basic/static/sphinx_highlight.js',
'tests/js/*.js'
'tests/js/*.js',
{ pattern: 'tests/js/fixtures/**/*.js', included: false, served: true }
],


Expand Down
32 changes: 21 additions & 11 deletions sphinx/themes/basic/static/searchtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,7 @@ const Search = {
else Search.deferQuery(query);
},

/**
* execute search (requires search index to be loaded)
*/
query: (query) => {
const filenames = Search._index.filenames;
const docNames = Search._index.docnames;
const titles = Search._index.titles;
const allTitles = Search._index.alltitles;
const indexEntries = Search._index.indexentries;

_parseQuery: (query) => {
// stem the search terms and add them to the correct list
const stemmer = new Stemmer();
const searchTerms = new Set();
Expand Down Expand Up @@ -313,6 +304,19 @@ const Search = {
// console.info("required: ", [...searchTerms]);
// console.info("excluded: ", [...excludedTerms]);

return [query, searchTerms, excludedTerms, highlightTerms, objectTerms];
},

/**
* execute search (requires search index to be loaded)
*/
_performSearch: (query, searchTerms, excludedTerms, highlightTerms, objectTerms) => {
const filenames = Search._index.filenames;
const docNames = Search._index.docnames;
const titles = Search._index.titles;
const allTitles = Search._index.alltitles;
const indexEntries = Search._index.indexentries;

// Collect multiple result groups to be sorted separately and then ordered.
// Each is an array of [docname, title, anchor, descr, score, filename].
const normalResults = [];
Expand Down Expand Up @@ -394,7 +398,13 @@ const Search = {
return acc;
}, []);

results = results.reverse();
return results.reverse();
},

query: (query) => {

const searchTerms = Search._parseQuery(query);
const results = Search._performSearch(...searchTerms);

// for debugging
//Search.lastresults = results.slice(); // a copy
Expand Down
1 change: 1 addition & 0 deletions tests/js/fixtures/cpp/searchindex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/js/fixtures/multiterm/searchindex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions tests/js/project/language_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* language_data.js
* ~~~~~~~~~~~~~~~~
*
* This script contains the language-specific data used by searchtools.js,
* namely the list of stopwords, stemmer, scorer and splitter.
*
* :copyright: Copyright 2007-2024 by the Sphinx team, see AUTHORS.
* :license: BSD, see LICENSE for details.
*
*/

var stopwords = [];


/* Non-minified version is copied as a separate JS file, is available */

/**
* Dummy stemmer for languages without stemming rules.
*/
var Stemmer = function() {
this.stemWord = function(w) {
return w;
}
}

Empty file added tests/js/roots/cpp/conf.py
Empty file.
5 changes: 5 additions & 0 deletions tests/js/roots/cpp/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This is a sample C++ project used to generate a search engine index fixture.

.. cpp:class:: public Sphinx

The description of Sphinx class.
Empty file.
4 changes: 4 additions & 0 deletions tests/js/roots/multiterm/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Main Page
=========

Welcome to the... main page!
72 changes: 35 additions & 37 deletions tests/js/searchtools.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
describe('Basic html theme search', function() {

function loadFixture(name) {
req = new XMLHttpRequest();
req.open("GET", `base/tests/js/fixtures/${name}`, false);
req.send(null);
return req.responseText;
}

describe('terms search', function() {

it('should find "C++" when in index', function() {
index = {
docnames:["index"],
filenames:["index.rst"],
terms:{'c++':0},
titles:["<no title>"],
titleterms:{}
}
Search.setIndex(index);
searchterms = ['c++'];
excluded = [];
terms = index.terms;
titleterms = index.titleterms;
eval(loadFixture("cpp/searchindex.js"));

searchTerms = Search._parseQuery('C++');

hits = [[
"index",
Expand All @@ -24,34 +22,34 @@ describe('Basic html theme search', function() {
5,
"index.rst"
]];
expect(Search.performTermsSearch(searchterms, excluded, terms, titleterms)).toEqual(hits);
expect(Search._performSearch(...searchTerms)).toEqual(hits);
});

it('should be able to search for multiple terms', function() {
index = {
alltitles: {
'Main Page': [[0, 'main-page']],
},
docnames:["index"],
filenames:["index.rst"],
terms:{main:0, page:0},
titles:["Main Page"],
titleterms:{ main:0, page:0 }
}
Search.setIndex(index);

searchterms = ['main', 'page'];
excluded = [];
terms = index.terms;
titleterms = index.titleterms;
hits = [[
'index',
'Main Page',
'',
null,
15,
'index.rst']];
expect(Search.performTermsSearch(searchterms, excluded, terms, titleterms)).toEqual(hits);
eval(loadFixture("multiterm/searchindex.js"));

searchTerms = Search._parseQuery('main page');

// fixme: duplicate result due to https://github.com/sphinx-doc/sphinx/issues/11961
hits = [
[
'index',
'Main Page',
'',
null,
15,
'index.rst'
],
[
'index',
'Main Page',
'#main-page',
null,
100,
'index.rst'
]
];
expect(Search._performSearch(...searchTerms)).toEqual(hits);
});

});
Expand Down
31 changes: 31 additions & 0 deletions utils/generate_js_fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python3

import subprocess
from pathlib import Path

SPHINX_ROOT = Path(__file__).resolve().parents[1]
TEST_JS_FIXTURES = SPHINX_ROOT / "tests" / "js" / "fixtures"
TEST_JS_ROOTS = SPHINX_ROOT / "tests" / "js" / "roots"


def build(srcdir: Path) -> None:
cmd = ("sphinx-build", "-E", "-q", "-b", "html", f"{srcdir}", f"{srcdir}/build")
subprocess.run(cmd, check=True, capture_output=True)


def beautify(filename: Path) -> None:
cmd = ("js-beautify", "-r", filename)
subprocess.run(cmd, check=True, capture_output=True)


for directory in TEST_JS_ROOTS.iterdir():
searchindex = directory / "build" / "searchindex.js"
destination = TEST_JS_FIXTURES / directory.name / "searchindex.js"

print(f"Building {directory} ... ", end="")
build(directory)
print("done")

print(f"Moving {searchindex} to {destination} ... ", end="")
searchindex.replace(destination)
print("done")