1#!/usr/bin/env python3
2
3import os
4import subprocess
5import sys
6import unittest
7
8CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
9ESP32_DOC = 'esp32_page'
10ESP32_S2_DOC = 'esp32s2_page'
11BT_DOC = 'bt_page'
12LINK_ROLES_DOC = 'link_roles'
13IDF_FORMAT_DOC = 'idf_target_format'
14
15
16class DocBuilder():
17    build_docs_py_path = os.path.join(CURRENT_DIR, '..', 'build_docs.py')
18
19    def __init__(self, src_dir, build_dir, target, language):
20        self.language = language
21        self.target = target
22        self.src_dir = src_dir
23        self.build_dir = build_dir
24        self.html_out_dir = os.path.join(CURRENT_DIR, build_dir, language, target, 'html')
25
26    def build(self, opt_args=[]):
27        args = [sys.executable, self.build_docs_py_path, '-b', self.build_dir, '-s', self.src_dir, '-t', self.target, '-l', self.language]
28        args.extend(opt_args)
29        return subprocess.call(args)
30
31
32class TestDocs(unittest.TestCase):
33
34    @classmethod
35    def setUpClass(cls):
36        cls.builder = DocBuilder('test', '_build/test_docs', 'esp32s2', 'en')
37        cls.build_ret_flag = cls.builder.build()
38
39    def setUp(self):
40        if self.build_ret_flag:
41            self.fail('Build docs failed with return: {}'.format(self.build_ret_flag))
42
43    def assert_str_not_in_doc(self, doc_name, str_to_find):
44        with open(os.path.join(self.builder.html_out_dir, doc_name)) as f:
45            content = f.read()
46            self.assertFalse(str_to_find in content, 'Found {} in {}'.format(str_to_find, doc_name))
47
48    def assert_str_in_doc(self, doc_name, str_to_find):
49        with open(os.path.join(self.builder.html_out_dir, doc_name)) as f:
50            content = f.read()
51            self.assertTrue(str_to_find in content, 'Did not find {} in {}'.format(str_to_find, doc_name))
52
53    def test_only_dir(self):
54        # Test that ESP32 content was excluded
55        self.assert_str_not_in_doc(ESP32_S2_DOC + '.html', '!ESP32_CONTENT!')
56
57        # Test that ESP32 S2 content was included
58        self.assert_str_in_doc(ESP32_S2_DOC + '.html', '!ESP32_S2_CONTENT!')
59
60        # Test that BT content was excluded
61        self.assert_str_not_in_doc(ESP32_S2_DOC + '.html', '!BT_CONTENT!')
62
63    def test_toctree_filter(self):
64        # ESP32 page should NOT be built
65        esp32_doc = os.path.join(self.builder.html_out_dir, ESP32_DOC + '.html')
66        self.assertFalse(os.path.isfile(esp32_doc), 'Found {}'.format(esp32_doc))
67        self.assert_str_not_in_doc('index.html', '!ESP32_CONTENT!')
68
69        esp32s2_doc = os.path.join(self.builder.html_out_dir, ESP32_S2_DOC + '.html')
70        self.assertTrue(os.path.isfile(esp32s2_doc), '{} not found'.format(esp32s2_doc))
71
72        # Spot check a few other tags
73        # No Bluetooth on ESP32 S2
74        bt_doc = os.path.join(self.builder.html_out_dir, BT_DOC + '.html')
75        self.assertFalse(os.path.isfile(bt_doc), 'Found {}'.format(bt_doc))
76        self.assert_str_not_in_doc('index.html', '!BT_CONTENT!')
77
78    def test_link_roles(self):
79        print('test')
80
81
82class TestBuildSubset(unittest.TestCase):
83    def test_build_subset(self):
84        builder = DocBuilder('test', '_build/test_build_subset', 'esp32', 'en')
85
86        docs_to_build = 'esp32_page.rst'
87
88        self.assertFalse(builder.build(['-i', docs_to_build]))
89
90        # Check that we only built the input docs
91        bt_doc = os.path.join(builder.html_out_dir, BT_DOC + '.html')
92        esp32_doc = os.path.join(builder.html_out_dir, ESP32_DOC + '.html')
93        self.assertFalse(os.path.isfile(bt_doc), 'Found {}'.format(bt_doc))
94        self.assertTrue(os.path.isfile(esp32_doc), 'Found {}'.format(esp32_doc))
95
96
97if __name__ == '__main__':
98    unittest.main()
99