Skip to content

Commit 13cc509

Browse files
committed
feat: Adapting tests and scripts to work on cross platform OS
1 parent ff450c4 commit 13cc509

File tree

6 files changed

+66
-13
lines changed

6 files changed

+66
-13
lines changed

Google.Api.Generator.Tests/Invoker.cs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ namespace Google.Api.Generator.Tests
2424
{
2525
internal static class Invoker
2626
{
27+
const string ProtocFileStr = "ProtocFile";
28+
const string RuntimeStr = "Runtime";
29+
const string PluginFileStr = "PluginFile";
30+
2731
public class WithPath : IDisposable
2832
{
2933
public WithPath(Action action, string path) => (_action, Path) = (action, path);
@@ -38,11 +42,13 @@ static Invoker()
3842
RootDir = PathUtils.GetRepoRoot();
3943
GeneratorDir = Path.Combine(RootDir, "Google.Api.Generator");
4044
GeneratorTestsDir = Path.Combine(RootDir, "Google.Api.Generator.Tests");
41-
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
42-
ProtocFile = Path.Combine(RootDir, "tools", isWindows ? "protoc.exe" : "protoc");
43-
Runtime = isWindows ? "win-x64" : "linux-x64";
44-
PluginFile = Path.Combine(GeneratorDir, "bin", "Debug", "net6.0", Runtime, "publish",
45-
isWindows ? "Google.Api.Generator.exe" : "Google.Api.Generator");
45+
46+
// Fetch OS dependent executable paths and other configs
47+
var OSConfigs = GetOSSpecificConfigs(RootDir);
48+
ProtocFile = OSConfigs["ProtocFile"];
49+
Runtime = OSConfigs["Runtime"];
50+
PluginFile = OSConfigs["PluginFile"];
51+
4652
CommonProtosDir = Path.Combine(RootDir, "googleapis");
4753
ProtobufDir = Path.Combine(RootDir, "tools", "protos");
4854
var now = DateTime.UtcNow;
@@ -92,7 +98,7 @@ private static void Execute(string executable, string args, string workingDirect
9298
process.OutputDataReceived += handler;
9399
process.BeginErrorReadLine();
94100
process.BeginOutputReadLine();
95-
var exited = process.WaitForExit((int) timeout.TotalMilliseconds);
101+
var exited = process.WaitForExit((int)timeout.TotalMilliseconds);
96102

97103
// Avoid any extra data being added to our output while we're processing assertions.
98104
process.ErrorDataReceived -= handler;
@@ -128,5 +134,35 @@ public static WithPath TempDir()
128134
Directory.CreateDirectory(dirPath);
129135
return new WithPath(() => Directory.Delete(dirPath, recursive: true), dirPath);
130136
}
137+
138+
private static Dictionary<string, string> GetOSSpecificConfigs(string rootDir)
139+
{
140+
Dictionary<string, string> configs = new Dictionary<string, string>();
141+
142+
143+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
144+
{
145+
// Linux configs
146+
configs[ProtocFileStr] = Path.Combine(RootDir, "tools", "protoc");
147+
configs[RuntimeStr] = "linux-x64";
148+
configs[PluginFileStr] = Path.Combine(GeneratorDir, "bin", "Debug", "net6.0", configs[RuntimeStr], "publish", "Google.Api.Generator");
149+
}
150+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
151+
{
152+
// MacOS configs
153+
configs[ProtocFileStr] = Path.Combine(RootDir, "tools", "protoc_macosx_x64");
154+
configs[RuntimeStr] = "osx-x64";
155+
configs[PluginFileStr] = Path.Combine(GeneratorDir, "bin", "Debug", "net6.0", configs[RuntimeStr], "publish", "Google.Api.Generator");
156+
}
157+
else
158+
{
159+
// Default to Windows configs
160+
configs[ProtocFileStr] = Path.Combine(RootDir, "tools", "protoc.exe");
161+
configs[RuntimeStr] = "win-x64";
162+
configs[PluginFileStr] = Path.Combine(GeneratorDir, "bin", "Debug", "net6.0", configs[RuntimeStr], "publish", "Google.Api.Generator.exe");
163+
}
164+
165+
return configs;
166+
}
131167
}
132168
}

generateprotos.sh

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,29 @@
66
# - protoc compiler plugin
77
# - Generator config protos (e.g. service config, snippets)
88
# - GAPIC ShowCase
9-
#
10-
# Currently this script is hard-coded to use protoc.exe,
11-
# so it only runs on Windows; we can use platform detection
12-
# if that ever becomes an issue.
9+
10+
# Cross-platform tools
11+
protoc_executable="tools/protoc.exe" # default to Windows
12+
grpc_csharp_executable="tools/grpc_csharp_plugin.exe" # default to Windows
13+
case "$OSTYPE" in
14+
linux*)
15+
protoc_executable="tools/protoc"
16+
grpc_csharp_executable="tools/grpc_csharp_plugin"
17+
;;
18+
darwin*)
19+
protoc_executable="tools/protoc_macosx_x64"
20+
grpc_csharp_executable="tools/grpc_csharp_plugin_macosx_x64"
21+
chmod +x tools/protoc_macosx_x64
22+
chmod +x tools/grpc_csharp_plugin_macosx_x64
23+
;;
24+
*)
25+
echo "Unknown OSTYPE: $OSTYPE"
26+
exit 1
27+
esac
1328

1429
# Generator config protos
1530
echo "Generating generator config protos"
16-
tools/protoc.exe \
31+
"$protoc_executable" \
1732
--csharp_out=Google.Api.Generator/ConfigProtos \
1833
-Itools/protos \
1934
-Igoogleapis \
@@ -22,10 +37,10 @@ tools/protoc.exe \
2237

2338
# GAPIC Showcase
2439
echo "Generating GAPIC Showcase"
25-
tools/protoc.exe \
40+
"$protoc_executable" \
2641
--csharp_out=Google.Api.Generator.Tests/ProtoTests/Showcase \
2742
--csharp_opt=file_extension=.g.cs \
28-
--plugin=protoc-gen-grpc=tools/grpc_csharp_plugin.exe \
43+
--plugin=protoc-gen-grpc="$grpc_csharp_executable" \
2944
--grpc_out=Google.Api.Generator.Tests/ProtoTests/Showcase \
3045
--grpc_opt=file_suffix=Grpc.g.cs \
3146
-Itools/protos \
2.74 MB
Binary file not shown.

tools/protoc_macosx_x64

7.85 MB
Binary file not shown.

tools/update-grpc.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ unzip -q tmp.zip
2323

2424
mv tools/windows_x64/grpc_csharp_plugin.exe ..
2525
mv tools/linux_x64/grpc_csharp_plugin ..
26+
mv tools/macosx_x64/grpc_csharp_plugin ../grpc_csharp_plugin_macosx_x64
2627

2728
cd ..
2829
rm -rf tmp

tools/update-protoc.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ unzip -q tmp.zip
2323

2424
mv tools/windows_x64/protoc.exe ..
2525
mv tools/linux_x64/protoc ..
26+
mv tools/macosx_x64/protoc ../protoc_macosx_x64
2627
cp tools/google/protobuf/* ../protos/google/protobuf
2728

2829
cd ..

0 commit comments

Comments
 (0)