File: test_python_snippet_updater.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (64 lines) | stat: -rw-r--r-- 1,752 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import tempfile
from ci_tools.snippet_update.python_snippet_updater import (
    get_snippet,
    update_snippet,
    check_snippets,
    check_not_up_to_date,
)


def test_update_snippet():
    temp_sample = tempfile.NamedTemporaryFile(delete=False)
    snippets = """
                 # [START trio]
                 from azure.core.pipeline.transport import TrioRequestsTransport

                 async with AsyncPipeline(TrioRequestsTransport(), policies=policies) as pipeline:
                     return await pipeline.run(request)
                 # [END trio]
    """
    temp_sample.write(snippets.encode("utf-8"))
    temp_sample.close()
    full_path_sample = temp_sample.name
    get_snippet(full_path_sample)
    parsed_snippets = check_snippets()
    print(parsed_snippets)
    assert len(parsed_snippets) == 1
    keys = parsed_snippets.keys()
    snippet_name = list(keys)[0]

    temp_readme = tempfile.NamedTemporaryFile(delete=False)
    readme = (
        """
<!-- SNIPPET:"""
        + snippet_name
        + """ -->

```python
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]

text_analytics_client = TextAnalyticsClient(endpoint, AzureKeyCredential(key))
```

<!-- END SNIPPET -->
        """
    )
    temp_readme.write(readme.encode("utf-8"))
    temp_readme.close()
    update_snippet(temp_readme.name)
    with open(temp_readme.name, "rb") as file:
        content = file.read()
        print(content)

    assert check_not_up_to_date()
    os.unlink(temp_readme.name)
    os.unlink(full_path_sample)


if __name__ == "__main__":
    test_update_snippet()