|
1 | | -import unittest |
2 | | -import subprocess |
3 | | -import os.path |
4 | | -import os |
5 | | -from typing import Tuple, List |
| 1 | +from protogen.test import run_plugin |
6 | 2 |
|
7 | 3 |
|
8 | 4 | def test_protogen(): |
9 | | - # test won't work if using relative path. protoc seems to have troubles |
10 | | - # finding files then: |
11 | | - # |
12 | | - # - protoc-gen-empty=./test/plugin/main.py: warning: directory does not exist. |
13 | | - # - vendor/google/api/annotations.proto: File does not reside within any path |
14 | | - # specified using --proto_path (or -I). You must specify a --proto_path which |
15 | | - # encompasses this file. Note that the proto_path must be an exact prefix of |
16 | | - # the .proto file names -- protoc is too dumb to figure out when two paths |
17 | | - # (e.g. absolute and relative) are equivalent (it's harder than you think). |
18 | | - # |
19 | | - pwd = os.getcwd() |
20 | | - vendor = os.path.join(pwd, "test", "vendor") |
21 | | - proto = os.path.join(pwd, "test", "vendor", "google", "api", "annotations.proto") |
22 | | - plugin = os.path.join(pwd, "test", "plugin", "main.py") |
| 5 | + resp = run_plugin( |
| 6 | + proto_paths=["test/vendor"], |
| 7 | + files_to_generate=["google/api/annotations.proto"], |
| 8 | + plugin="test.plugin.main", |
| 9 | + ) |
23 | 10 |
|
24 | | - # Create the output directory if it does not exists. protoc requires it |
25 | | - # to exist before generating output to it. |
26 | | - out_dir = os.path.join(pwd, "testout", "main") |
27 | | - if not os.path.exists(out_dir): |
28 | | - os.makedirs(out_dir) |
| 11 | + assert resp.proto.error == "" |
29 | 12 |
|
30 | | - # Run protoc. |
31 | | - code, output = _run_protoc( |
32 | | - [ |
33 | | - "protoc", |
34 | | - "-I", |
35 | | - vendor, |
36 | | - f"--plugin=protoc-gen-empty={plugin}", |
37 | | - f"--empty_out={out_dir}", |
38 | | - proto, |
39 | | - ] |
40 | | - ) |
41 | | - assert output == "" |
42 | | - assert code == 0 |
| 13 | + generated, ok = resp.file_content("google/api/annotations.out") |
| 14 | + assert ok |
43 | 15 |
|
44 | | - # Check content of directory. |
45 | 16 | with open("test/golden/google/api/annotations.out") as f: |
46 | 17 | golden = f.read() |
47 | | - with open("testout/main/google/api/annotations.out") as f: |
48 | | - generated = f.read() |
49 | 18 |
|
50 | 19 | assert golden == generated |
51 | 20 |
|
52 | 21 |
|
53 | 22 | def test_parameter(): |
54 | | - pwd = os.getcwd() |
55 | | - vendor = os.path.join(pwd, "test", "vendor") |
56 | | - proto = os.path.join(pwd, "test", "vendor", "google", "api", "annotations.proto") |
57 | | - plugin = os.path.join(pwd, "test", "plugin", "parameter.py") |
| 23 | + resp = run_plugin( |
| 24 | + proto_paths=["test/vendor"], |
| 25 | + files_to_generate=["google/api/annotations.proto"], |
| 26 | + plugin="test.plugin.parameter", |
| 27 | + parameter={ |
| 28 | + "k1": "v1", |
| 29 | + "k2": "v2;k3=v3", |
| 30 | + "k4": "", |
| 31 | + "k5": "v5", |
| 32 | + "abc": "x", |
| 33 | + "5": "2", |
| 34 | + }, |
| 35 | + ) |
58 | 36 |
|
59 | | - # Create the output directory if it does not exists. protoc requires it |
60 | | - # to exist before generating output to it. |
61 | | - out_dir = os.path.join(pwd, "testout", "params") |
62 | | - if not os.path.exists(out_dir): |
63 | | - os.makedirs(out_dir) |
| 37 | + assert resp.proto.error == "" |
64 | 38 |
|
65 | | - code, output = _run_protoc( |
66 | | - [ |
67 | | - "protoc", |
68 | | - "-I", |
69 | | - vendor, |
70 | | - f"--plugin=protoc-gen-params={plugin}", |
71 | | - f"--params_opt=k1=v1,k2=v2;k3=v3,", |
72 | | - f"--params_opt=k4", |
73 | | - f"--params_opt=k5=v5", |
74 | | - f"--params_out=abc=x,5=2:{out_dir}", |
75 | | - proto, |
76 | | - ] |
77 | | - ) |
78 | | - assert output == "" |
79 | | - assert code == 0 |
| 39 | + generated, ok = resp.file_content("out.txt") |
| 40 | + assert ok |
80 | 41 |
|
81 | | - # Check content of directory. |
82 | 42 | with open("test/golden/params.txt") as f: |
83 | 43 | golden = f.read() |
84 | | - with open("testout/params/out.txt") as f: |
85 | | - generated = f.read() |
86 | 44 |
|
87 | 45 | assert golden == generated |
88 | | - |
89 | | - |
90 | | -def _run_protoc(args: List[str]) -> Tuple[int, str]: |
91 | | - proc = subprocess.Popen( |
92 | | - args, text=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE |
93 | | - ) |
94 | | - # Executed from current directory (repo root) |
95 | | - code = proc.wait() |
96 | | - if code == 0: |
97 | | - output = proc.stdout.read() |
98 | | - else: |
99 | | - output = proc.stderr.read() |
100 | | - proc.terminate() # TODO necessary? |
101 | | - return code, output |
0 commit comments