From 350fbef48629d0ab67cdf5e92a646d61a52445d7 Mon Sep 17 00:00:00 2001 From: Serge K Date: Sat, 15 Jun 2024 18:06:22 +0700 Subject: [PATCH] test: to unix line endings --- tests/Extensions.cs | 14 ++++++++ ...rometheus.Client.HealthChecks.Tests.csproj | 9 ++--- tests/PrometheusHealthCheckPublisherTests.cs | 35 +++++++++---------- 3 files changed, 33 insertions(+), 25 deletions(-) create mode 100644 tests/Extensions.cs diff --git a/tests/Extensions.cs b/tests/Extensions.cs new file mode 100644 index 0000000..c38d21f --- /dev/null +++ b/tests/Extensions.cs @@ -0,0 +1,14 @@ +using System; + +namespace Prometheus.Client.HealthChecks.Tests; + +public static class Extensions +{ + private const string _unix = "\n"; + private const string _nonUnix = "\r\n"; + + public static string ToUnixLineEndings(this string s) + { + return Environment.NewLine == _unix ? s : s.Replace(_nonUnix, _unix); + } +} diff --git a/tests/Prometheus.Client.HealthChecks.Tests.csproj b/tests/Prometheus.Client.HealthChecks.Tests.csproj index 67d97f4..8fd72d7 100644 --- a/tests/Prometheus.Client.HealthChecks.Tests.csproj +++ b/tests/Prometheus.Client.HealthChecks.Tests.csproj @@ -1,16 +1,11 @@  + net6.0;net8.0 + net462;net6.0;net8.0 false true $(NoWarn);CS0618 - - net6.0;net8.0 - - - net462;net6.0;net8.0 - $(DefineConstants);Windows - diff --git a/tests/PrometheusHealthCheckPublisherTests.cs b/tests/PrometheusHealthCheckPublisherTests.cs index 2b8f380..b919d12 100644 --- a/tests/PrometheusHealthCheckPublisherTests.cs +++ b/tests/PrometheusHealthCheckPublisherTests.cs @@ -14,6 +14,7 @@ public class PrometheusHealthCheckPublisherTests { private readonly ICollectorRegistry _registry; private readonly PrometheusHealthCheckPublisherOptions _options; + private PrometheusHealthCheckPublisher _publisher; public PrometheusHealthCheckPublisherTests() { @@ -27,7 +28,7 @@ public PrometheusHealthCheckPublisherTests() [Fact] public void Publisher_Register_Metrics() { - var publisher = new PrometheusHealthCheckPublisher(_options); + _publisher = new PrometheusHealthCheckPublisher(_options); Assert.True(_registry.TryGet(PrometheusHealthCheckPublisherOptions.DefaultStatusMetricName, out _)); Assert.True(_registry.TryGet(PrometheusHealthCheckPublisherOptions.DefaultDurationMetricName, out _)); } @@ -40,8 +41,7 @@ public void Publisher_Register_Metrics_With_CustomNames(string statusMetricName, { _options.StatusMetricName = statusMetricName; _options.DurationMetricName = durationMetricName; - - var publisher = new PrometheusHealthCheckPublisher(_options); + _publisher = new PrometheusHealthCheckPublisher(_options); Assert.True(_registry.TryGet(statusMetricName, out _)); Assert.True(_registry.TryGet(durationMetricName, out _)); } @@ -53,33 +53,32 @@ public void Publisher_Register_Metrics_With_CustomNames(string statusMetricName, [InlineData("key", HealthStatus.Degraded, 1)] public async Task Publisher_Publish_Correct_Result(string key, HealthStatus status, int durationSec) { - const string tmpl = """ - # HELP [[durationMetricName]] Shows duration of the health check execution in seconds - # TYPE [[durationMetricName]] gauge - [[durationMetricName]]{name="[[key]]"} [[duration]] - # HELP [[statusMetricName]] Shows raw health check status (0 = Unhealthy, 1 = Degraded, 2 = Healthy) - # TYPE [[statusMetricName]] gauge - [[statusMetricName]]{name="[[key]]"} [[status]] + var expected = """ + # HELP [[durationMetricName]] Shows duration of the health check execution in seconds + # TYPE [[durationMetricName]] gauge + [[durationMetricName]]{name="[[key]]"} [[duration]] + # HELP [[statusMetricName]] Shows raw health check status (0 = Unhealthy, 1 = Degraded, 2 = Healthy) + # TYPE [[statusMetricName]] gauge + [[statusMetricName]]{name="[[key]]"} [[status]] + + """; - """; - var expected = tmpl + expected = expected .Replace("[[durationMetricName]]", PrometheusHealthCheckPublisherOptions.DefaultDurationMetricName) .Replace("[[statusMetricName]]", PrometheusHealthCheckPublisherOptions.DefaultStatusMetricName) .Replace("[[key]]", key) .Replace("[[status]]", ((int)status).ToString()) - .Replace("[[duration]]", durationSec.ToString()); + .Replace("[[duration]]", durationSec.ToString()) + .ToUnixLineEndings(); -#if Windows - expected = expected.Replace("\r\n", "\n"); -#endif - var publisher = new PrometheusHealthCheckPublisher(_options); + _publisher = new PrometheusHealthCheckPublisher(_options); var entries = new Dictionary { { key, new HealthReportEntry(status, string.Empty, TimeSpan.FromSeconds(durationSec), null, null) } }; var report = new HealthReport(entries, TimeSpan.FromSeconds(1)); - await publisher.PublishAsync(report, CancellationToken.None); + await _publisher.PublishAsync(report, CancellationToken.None); using var stream = new MemoryStream(); await ScrapeHandler.ProcessAsync(_registry, stream);