|
| 1 | +// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt |
| 2 | + |
| 3 | +using System; |
| 4 | +using NUnit.Engine; |
| 5 | +using NUnit.Framework; |
| 6 | + |
| 7 | +namespace NUnit.Agents |
| 8 | +{ |
| 9 | + public class AgentOptionTests |
| 10 | + { |
| 11 | + static TestCaseData[] DefaultSettings = new[] |
| 12 | + { |
| 13 | + new TestCaseData("AgentId", Guid.Empty), |
| 14 | + new TestCaseData("AgencyUrl", string.Empty), |
| 15 | + new TestCaseData("AgencyPid", string.Empty), |
| 16 | + new TestCaseData("DebugAgent", false), |
| 17 | + new TestCaseData("DebugTests", false), |
| 18 | + new TestCaseData("TraceLevel", InternalTraceLevel.Off), |
| 19 | + new TestCaseData("WorkDirectory", string.Empty) |
| 20 | + }; |
| 21 | + |
| 22 | + [TestCaseSource(nameof(DefaultSettings))] |
| 23 | + public void DefaultOptionSettings<T>(string propertyName, T defaultValue) |
| 24 | + { |
| 25 | + var options = new AgentOptions(); |
| 26 | + var prop = typeof(AgentOptions).GetProperty(propertyName); |
| 27 | + Assert.That(prop, Is.Not.Null, $"Property {propertyName} does not exist"); |
| 28 | + Assert.That(prop.GetValue(options, new object[0]), Is.EqualTo(defaultValue)); |
| 29 | + } |
| 30 | + |
| 31 | + static readonly Guid AGENT_GUID = Guid.NewGuid(); |
| 32 | + static readonly TestCaseData[] ValidSettings = new[] |
| 33 | + { |
| 34 | + // Boolean options - no values provided |
| 35 | + new TestCaseData("--debug-agent", "DebugAgent", true), |
| 36 | + new TestCaseData("--debug-tests", "DebugTests", true), |
| 37 | + // Options with values - using '=' as delimiter |
| 38 | + new TestCaseData($"--agentId={AGENT_GUID}", "AgentId", AGENT_GUID), |
| 39 | + new TestCaseData("--agencyUrl=THEURL", "AgencyUrl", "THEURL"), |
| 40 | + new TestCaseData("--pid=1234", "AgencyPid", "1234"), |
| 41 | + new TestCaseData("--trace=Info", "TraceLevel", InternalTraceLevel.Info), |
| 42 | + new TestCaseData("--work=WORKDIR", "WorkDirectory", "WORKDIR"), |
| 43 | + // Options with values - using ':' as delimiter |
| 44 | + new TestCaseData("--trace:Error", "TraceLevel", InternalTraceLevel.Error), |
| 45 | + new TestCaseData("--work:WORKDIR", "WorkDirectory", "WORKDIR"), |
| 46 | + // Value with spaces (provided OS passes them through) |
| 47 | + new TestCaseData("--work:MY WORK DIR", "WorkDirectory", "MY WORK DIR"), |
| 48 | + }; |
| 49 | + |
| 50 | + [TestCaseSource(nameof(ValidSettings))] |
| 51 | + public void ValidOptionSettings<T>(string option, string propertyName, T expectedValue) |
| 52 | + { |
| 53 | + var options = new AgentOptions(option); |
| 54 | + var prop = typeof(AgentOptions).GetProperty(propertyName); |
| 55 | + Assert.That(prop, Is.Not.Null, $"Property {propertyName} does not exist"); |
| 56 | + Assert.That(prop.GetValue(options, new object[0]), Is.EqualTo(expectedValue)); |
| 57 | + } |
| 58 | + |
| 59 | + [Test] |
| 60 | + public void MultipleOptions() |
| 61 | + { |
| 62 | + var options = new AgentOptions("--debug-tests", "--trace=Info", "--work", "MYWORKDIR"); |
| 63 | + Assert.That(options.DebugAgent, Is.False); |
| 64 | + Assert.That(options.DebugTests); |
| 65 | + Assert.That(options.TraceLevel, Is.EqualTo(InternalTraceLevel.Info)); |
| 66 | + Assert.That(options.WorkDirectory, Is.EqualTo("MYWORKDIR")); |
| 67 | + } |
| 68 | + |
| 69 | + [Test] |
| 70 | + public void FileNameSupplied() |
| 71 | + { |
| 72 | + var filename = GetType().Assembly.Location; |
| 73 | + var options = new AgentOptions(filename); |
| 74 | + Assert.That(options.Files.Count, Is.EqualTo(1)); |
| 75 | + Assert.That(options.Files[0], Is.EqualTo(filename)); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments