|
17 | 17 | import os |
18 | 18 | from pathlib import Path |
19 | 19 | from synthtool.languages import node |
| 20 | +import pathlib |
| 21 | +import filecmp |
| 22 | +import pytest |
20 | 23 |
|
21 | 24 | FIXTURES = Path(__file__).parent / "fixtures" |
22 | 25 |
|
@@ -74,6 +77,102 @@ def test_no_samples(): |
74 | 77 | os.chdir(cwd) |
75 | 78 |
|
76 | 79 |
|
| 80 | +def test_extract_clients_no_file(): |
| 81 | + index_ts_path = pathlib.Path( |
| 82 | + FIXTURES / "node_templates" / "index_samples" / "no_exist_index.ts" |
| 83 | + ) |
| 84 | + |
| 85 | + with pytest.raises(FileNotFoundError): |
| 86 | + clients = node.extract_clients(index_ts_path) |
| 87 | + assert not clients |
| 88 | + |
| 89 | + |
| 90 | +def test_extract_single_clients(): |
| 91 | + index_ts_path = pathlib.Path( |
| 92 | + FIXTURES / "node_templates" / "index_samples" / "single_index.ts" |
| 93 | + ) |
| 94 | + |
| 95 | + clients = node.extract_clients(index_ts_path) |
| 96 | + |
| 97 | + assert len(clients) == 1 |
| 98 | + assert clients[0] == "TextToSpeechClient" |
| 99 | + |
| 100 | + |
| 101 | +def test_extract_multiple_clients(): |
| 102 | + index_ts_path = pathlib.Path( |
| 103 | + FIXTURES / "node_templates" / "index_samples" / "multiple_index.ts" |
| 104 | + ) |
| 105 | + |
| 106 | + clients = node.extract_clients(index_ts_path) |
| 107 | + |
| 108 | + assert len(clients) == 2 |
| 109 | + assert clients[0] == "StreamingVideoIntelligenceServiceClient" |
| 110 | + assert clients[1] == "VideoIntelligenceServiceClient" |
| 111 | + |
| 112 | + |
| 113 | +def test_generate_index_ts(): |
| 114 | + cwd = os.getcwd() |
| 115 | + try: |
| 116 | + # use a non-nodejs template directory |
| 117 | + os.chdir(FIXTURES / "node_templates" / "index_samples") |
| 118 | + node.generate_index_ts(["v1", "v1beta1"], "v1") |
| 119 | + generated_index_path = pathlib.Path( |
| 120 | + FIXTURES / "node_templates" / "index_samples" / "src" / "index.ts" |
| 121 | + ) |
| 122 | + sample_index_path = pathlib.Path( |
| 123 | + FIXTURES / "node_templates" / "index_samples" / "sample_index.ts" |
| 124 | + ) |
| 125 | + assert filecmp.cmp(generated_index_path, sample_index_path) |
| 126 | + finally: |
| 127 | + os.chdir(cwd) |
| 128 | + |
| 129 | + |
| 130 | +def test_generate_index_ts_empty_versions(): |
| 131 | + cwd = os.getcwd() |
| 132 | + try: |
| 133 | + # use a non-nodejs template directory |
| 134 | + os.chdir(FIXTURES / "node_templates" / "index_samples") |
| 135 | + |
| 136 | + with pytest.raises(AttributeError) as err: |
| 137 | + node.generate_index_ts([], "v1") |
| 138 | + assert "can't be empty" in err.args |
| 139 | + finally: |
| 140 | + os.chdir(cwd) |
| 141 | + |
| 142 | + |
| 143 | +def test_generate_index_ts_invalid_default_version(): |
| 144 | + cwd = os.getcwd() |
| 145 | + try: |
| 146 | + # use a non-nodejs template directory |
| 147 | + os.chdir(FIXTURES / "node_templates" / "index_samples") |
| 148 | + versions = ["v1beta1"] |
| 149 | + default_version = "v1" |
| 150 | + |
| 151 | + with pytest.raises(AttributeError) as err: |
| 152 | + node.generate_index_ts(versions, default_version) |
| 153 | + assert f"must contain default version {default_version}" in err.args |
| 154 | + finally: |
| 155 | + os.chdir(cwd) |
| 156 | + |
| 157 | + |
| 158 | +def test_generate_index_ts_no_clients(): |
| 159 | + cwd = os.getcwd() |
| 160 | + try: |
| 161 | + # use a non-nodejs template directory |
| 162 | + os.chdir(FIXTURES / "node_templates" / "index_samples") |
| 163 | + versions = ["v1", "v1beta1", "invalid_index"] |
| 164 | + default_version = "invalid_index" |
| 165 | + |
| 166 | + with pytest.raises(AttributeError) as err: |
| 167 | + node.generate_index_ts(versions, default_version) |
| 168 | + assert ( |
| 169 | + f"No client is exported in the default version's({default_version}) index.ts ." |
| 170 | + in err.args |
| 171 | + ) |
| 172 | + finally: |
| 173 | + os.chdir(cwd) |
| 174 | + |
| 175 | + |
77 | 176 | class TestPostprocess(TestCase): |
78 | 177 | @patch("synthtool.shell.run") |
79 | 178 | def test_install(self, shell_run_mock): |
|
0 commit comments