Skip to content

Commit 48a7b65

Browse files
Support Linux brand versions
1 parent 5065bc9 commit 48a7b65

File tree

3 files changed

+293
-0
lines changed

3 files changed

+293
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using BenchmarkDotNet.Extensions;
5+
6+
namespace BenchmarkDotNet.Helpers
7+
{
8+
internal static class LinuxOsReleaseHelper
9+
{
10+
public static string? GetNameByOsRelease(string[] lines)
11+
{
12+
try
13+
{
14+
var values = new Dictionary<string, string>();
15+
foreach (string line in lines)
16+
{
17+
string[] parts = line.Split(new[] {'='}, 2);
18+
19+
if (parts.Length == 2)
20+
{
21+
string key = parts[0].Trim();
22+
string value = parts[1].Trim();
23+
24+
// remove quotes if value is quoted
25+
if (value.Length >= 2 &&
26+
((value.StartsWith("\"") && value.EndsWith("\"")) ||
27+
(value.StartsWith("'") && value.EndsWith("'"))))
28+
{
29+
value = value.Substring(1, value.Length - 2);
30+
}
31+
32+
values[key] = value;
33+
}
34+
}
35+
36+
string? id = values.GetValueOrDefault("ID");
37+
string? name = values.GetValueOrDefault("NAME");
38+
string? version = values.GetValueOrDefault("VERSION");
39+
40+
string[] idsWithExtendedVersion = { "ubuntu", "linuxmint", "solus", "kali" };
41+
if (idsWithExtendedVersion.Contains(id) && !string.IsNullOrEmpty(version) && !string.IsNullOrEmpty(name))
42+
return name + " " + version;
43+
44+
string? prettyName = values.GetValueOrDefault("PRETTY_NAME");
45+
if (prettyName != null)
46+
return prettyName;
47+
48+
if (name != null && version != null)
49+
return name + " " + version;
50+
51+
if (name != null)
52+
return name;
53+
54+
if (id != null)
55+
return id;
56+
57+
return null;
58+
}
59+
catch (Exception)
60+
{
61+
return null;
62+
}
63+
}
64+
}
65+
}

src/BenchmarkDotNet/Portability/RuntimeInformation.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Diagnostics.CodeAnalysis;
5+
using System.IO;
56
using System.Linq;
67
using System.Management;
78
using System.Numerics;
@@ -163,6 +164,13 @@ public static string GetOsVersion()
163164
return OsBrandStringHelper.PrettifyMacOSX(systemVersion, kernelVersion);
164165
}
165166

167+
if (IsLinux())
168+
{
169+
string? version = GetLinuxOsVersion();
170+
if (version != null && version.Trim() != "")
171+
return version;
172+
}
173+
166174
string operatingSystem = RuntimeEnvironment.OperatingSystem;
167175
string operatingSystemVersion = RuntimeEnvironment.OperatingSystemVersion;
168176

@@ -172,6 +180,20 @@ public static string GetOsVersion()
172180
GetWindowsUbr());
173181
}
174182

183+
private static string? GetLinuxOsVersion()
184+
{
185+
if (!IsLinux())
186+
return null;
187+
try
188+
{
189+
return LinuxOsReleaseHelper.GetNameByOsRelease(File.ReadAllLines("/etc/os-release"));
190+
}
191+
catch (Exception)
192+
{
193+
return null;
194+
}
195+
}
196+
175197
// TODO: Introduce a common util API for registry calls, use it also in BenchmarkDotNet.Toolchains.CsProj.GetCurrentVersionBasedOnWindowsRegistry
176198
/// <summary>
177199
/// On Windows, this method returns UBR (Update Build Revision) based on Registry.
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
using System;
2+
using BenchmarkDotNet.Helpers;
3+
using Xunit;
4+
5+
namespace BenchmarkDotNet.Tests.Helpers
6+
{
7+
public class LinuxOsReleaseHelperTests
8+
{
9+
[Theory]
10+
[InlineData("""
11+
NAME="Ubuntu"
12+
VERSION="20.04.1 LTS (Focal Fossa)"
13+
ID=ubuntu
14+
ID_LIKE=debian
15+
PRETTY_NAME="Ubuntu 20.04.1 LTS"
16+
VERSION_ID="20.04"
17+
HOME_URL="https://www.ubuntu.com/"
18+
SUPPORT_URL="https://help.ubuntu.com/"
19+
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
20+
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
21+
VERSION_CODENAME=focal
22+
UBUNTU_CODENAME=focal
23+
""", "Ubuntu 20.04.1 LTS (Focal Fossa)")]
24+
[InlineData("""
25+
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
26+
NAME="Debian GNU/Linux"
27+
VERSION_ID="10"
28+
VERSION="10 (buster)"
29+
VERSION_CODENAME=buster
30+
ID=debian
31+
HOME_URL="https://www.debian.org/"
32+
SUPPORT_URL="https://www.debian.org/support"
33+
BUG_REPORT_URL="https://bugs.debian.org/"
34+
""", "Debian GNU/Linux 10 (buster)")]
35+
[InlineData("""
36+
NAME=Fedora
37+
VERSION="32 (Thirty Two)"
38+
ID=fedora
39+
VERSION_ID=32
40+
VERSION_CODENAME=""
41+
PLATFORM_ID="platform:f32"
42+
PRETTY_NAME="Fedora 32 (Thirty Two)"
43+
ANSI_COLOR="0;34"
44+
LOGO=fedora-logo-icon
45+
CPE_NAME="cpe:/o:fedoraproject:fedora:32"
46+
HOME_URL="https://fedoraproject.org/"
47+
DOCUMENTATION_URL="https://docs.fedoraproject.org/en-US/fedora/f32/system-administrators-guide/"
48+
SUPPORT_URL="https://fedoraproject.org/wiki/Communicating_and_getting_help"
49+
BUG_REPORT_URL="https://bugzilla.redhat.com/"
50+
REDHAT_BUGZILLA_PRODUCT="Fedora"
51+
REDHAT_BUGZILLA_PRODUCT_VERSION=32
52+
REDHAT_SUPPORT_PRODUCT="Fedora"
53+
REDHAT_SUPPORT_PRODUCT_VERSION=32
54+
PRIVACY_POLICY_URL="https://fedoraproject.org/wiki/Legal:PrivacyPolicy"
55+
""", "Fedora 32 (Thirty Two)")]
56+
[InlineData("""
57+
NAME="CentOS Linux"
58+
VERSION="8 (Core)"
59+
ID="centos"
60+
ID_LIKE="rhel fedora"
61+
VERSION_ID="8"
62+
PLATFORM_ID="platform:el8"
63+
PRETTY_NAME="CentOS Linux 8 (Core)"
64+
ANSI_COLOR="0;31"
65+
CPE_NAME="cpe:/o:centos:centos:8"
66+
HOME_URL="https://www.centos.org/"
67+
BUG_REPORT_URL="https://bugs.centos.org/"
68+
69+
CENTOS_MANTISBT_PROJECT="CentOS-8"
70+
CENTOS_MANTISBT_PROJECT_VERSION="8"
71+
REDHAT_SUPPORT_PRODUCT="centos"
72+
REDHAT_SUPPORT_PRODUCT_VERSION="8"
73+
""", "CentOS Linux 8 (Core)")]
74+
[InlineData("""
75+
NAME="Arch Linux"
76+
PRETTY_NAME="Arch Linux"
77+
ID=arch
78+
BUILD_ID=rolling
79+
ANSI_COLOR="38;2;23;147;209"
80+
HOME_URL="https://archlinux.org/"
81+
DOCUMENTATION_URL="https://wiki.archlinux.org/"
82+
SUPPORT_URL="https://bbs.archlinux.org/"
83+
BUG_REPORT_URL="https://bugs.archlinux.org/"
84+
LOGO=archlinux
85+
""", "Arch Linux")]
86+
[InlineData("""
87+
NAME="openSUSE Leap"
88+
VERSION="15.2"
89+
ID="opensuse-leap"
90+
ID_LIKE="suse opensuse"
91+
VERSION_ID="15.2"
92+
PRETTY_NAME="openSUSE Leap 15.2"
93+
ANSI_COLOR="0;32"
94+
CPE_NAME="cpe:/o:opensuse:leap:15.2"
95+
BUG_REPORT_URL="https://bugs.opensuse.org"
96+
HOME_URL="https://www.opensuse.org/"
97+
""", "openSUSE Leap 15.2")]
98+
[InlineData("""
99+
NAME="Manjaro Linux"
100+
ID=manjaro
101+
PRETTY_NAME="Manjaro Linux"
102+
ANSI_COLOR="1;32"
103+
HOME_URL="https://manjaro.org/"
104+
SUPPORT_URL="https://manjaro.org/"
105+
BUG_REPORT_URL="https://bugs.manjaro.org/"
106+
LOGO=manjarolinux
107+
""", "Manjaro Linux")]
108+
[InlineData("""
109+
NAME="Linux Mint"
110+
VERSION="20 (Ulyana)"
111+
ID=linuxmint
112+
ID_LIKE=ubuntu
113+
PRETTY_NAME="Linux Mint 20"
114+
VERSION_ID="20"
115+
HOME_URL="https://www.linuxmint.com/"
116+
SUPPORT_URL="https://forums.linuxmint.com/"
117+
BUG_REPORT_URL="http://linuxmint-troubleshooting-guide.readthedocs.io/en/latest/"
118+
PRIVACY_POLICY_URL="https://www.linuxmint.com/"
119+
VERSION_CODENAME=ulyana
120+
UBUNTU_CODENAME=focal
121+
""", "Linux Mint 20 (Ulyana)")]
122+
[InlineData("""
123+
NAME="Alpine Linux"
124+
ID=alpine
125+
VERSION_ID=3.12.0
126+
PRETTY_NAME="Alpine Linux v3.12"
127+
HOME_URL="https://alpinelinux.org/"
128+
BUG_REPORT_URL="https://bugs.alpinelinux.org/"
129+
""", "Alpine Linux v3.12")]
130+
[InlineData("""
131+
NAME="Solus"
132+
VERSION="4.1"
133+
ID="solus"
134+
PRETTY_NAME="Solus"
135+
ANSI_COLOR="1;34"
136+
HOME_URL="https://getsol.us"
137+
SUPPORT_URL="https://getsol.us/articles/contributing/getting-involved/en/"
138+
BUG_REPORT_URL="https://dev.getsol.us/"
139+
""", "Solus 4.1")]
140+
[InlineData("""
141+
NAME="Red Hat Enterprise Linux"
142+
VERSION="8.2 (Ootpa)"
143+
ID="rhel"
144+
ID_LIKE="fedora"
145+
VERSION_ID="8.2"
146+
PLATFORM_ID="platform:el8"
147+
PRETTY_NAME="Red Hat Enterprise Linux 8.2 (Ootpa)"
148+
ANSI_COLOR="0;31"
149+
CPE_NAME="cpe:/o:redhat:enterprise_linux:8.2:GA"
150+
HOME_URL="https://www.redhat.com/"
151+
BUG_REPORT_URL="https://bugzilla.redhat.com/"
152+
REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"
153+
REDHAT_BUGZILLA_PRODUCT_VERSION=8.2
154+
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
155+
REDHAT_SUPPORT_PRODUCT_VERSION="8.2"
156+
""", "Red Hat Enterprise Linux 8.2 (Ootpa)")]
157+
[InlineData("""
158+
PRETTY_NAME="Kali GNU/Linux Rolling"
159+
NAME="Kali GNU/Linux"
160+
ID=kali
161+
VERSION="2020.2"
162+
VERSION_ID="2020.2"
163+
VERSION_CODENAME="kali-rolling"
164+
ID_LIKE=debian
165+
ANSI_COLOR="1;31"
166+
HOME_URL="https://www.kali.org/"
167+
SUPPORT_URL="https://forums.kali.org/"
168+
BUG_REPORT_URL="https://bugs.kali.org/"
169+
""", "Kali GNU/Linux 2020.2")]
170+
[InlineData("""
171+
NAME="elementary OS"
172+
VERSION="5.1.7 Hera"
173+
ID=elementary
174+
ID_LIKE=ubuntu
175+
PRETTY_NAME="elementary OS 5.1.7 Hera"
176+
LOGO=distributor-logo
177+
VERSION_ID="5.1.7"
178+
HOME_URL="https://elementary.io/"
179+
SUPPORT_URL="https://elementary.io/support"
180+
BUG_REPORT_URL="https://github.com/elementary/os/issues/new"
181+
PRIVACY_POLICY_URL="https://elementary.io/privacy-policy"
182+
VERSION_CODENAME=hera
183+
UBUNTU_CODENAME=bionic
184+
""", "elementary OS 5.1.7 Hera")]
185+
[InlineData("""
186+
NAME="Zorin OS"
187+
VERSION="15.2"
188+
ID=zorin
189+
ID_LIKE=ubuntu
190+
PRETTY_NAME="Zorin OS 15.2"
191+
VERSION_ID="15"
192+
HOME_URL="https://www.zorinos.com/"
193+
SUPPORT_URL="https://www.zorinos.com/help"
194+
BUG_REPORT_URL="https://www.zorinos.com/contact"
195+
PRIVACY_POLICY_URL="https://www.zorinos.com/legal/privacy"
196+
VERSION_CODENAME=bionic
197+
UBUNTU_CODENAME=bionic
198+
""", "Zorin OS 15.2")]
199+
public void LinuxOsReleaseHelperTest(string osReleaseContent, string expectedName)
200+
{
201+
string[] lines = osReleaseContent.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
202+
string actualName = LinuxOsReleaseHelper.GetNameByOsRelease(lines);
203+
Assert.Equal(expectedName, actualName);
204+
}
205+
}
206+
}

0 commit comments

Comments
 (0)