Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Environment.Exit.cs « System « tests « System.Runtime.Extensions « src - github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 20d029c9c506def0aefa4efbc8ecfca43eadfbf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Xunit;

namespace System.Tests
{
    public class Environment_Exit : RemoteExecutorTestBase
    {
        public static object[][] ExitCodeValues = new object[][]
        {
            new object[] { 0 },
            new object[] { 1 },
            new object[] { 42 },
            new object[] { -1 },
            new object[] { -45 },
            new object[] { 255 },
        };

        [Theory]
        [MemberData(nameof(ExitCodeValues))]
        [ActiveIssue("https://github.com/dotnet/corefx/issues/19909 - RemoteInvoke returns a null Process on UapAot.", TargetFrameworkMonikers.UapAot)]
        public static void CheckExitCode(int expectedExitCode)
        {
            using (Process p = RemoteInvoke(s => int.Parse(s), expectedExitCode.ToString()).Process)
            {
                Assert.True(p.WaitForExit(30 * 1000));
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    Assert.Equal(expectedExitCode, p.ExitCode);
                }
                else
                {
                    Assert.Equal(unchecked((sbyte)expectedExitCode), unchecked((sbyte)p.ExitCode));
                }
            }
        }

        [Theory]
        [MemberData(nameof(ExitCodeValues))]
        public static void ExitCode_Roundtrips(int exitCode)
        {
            Environment.ExitCode = exitCode;
            Assert.Equal(exitCode, Environment.ExitCode);

            Environment.ExitCode = 0; // in case the test host has a void returning Main
        }

        [Theory]
        [InlineData(1)] // setting ExitCode and exiting Main
        [InlineData(2)] // setting ExitCode both from Main and from an Unloading event handler.
        [InlineData(3)] // using Exit(exitCode)
        [ActiveIssue("https://github.com/dotnet/corefx/issues/20387 - ILC test pipeline does not accomodate tests in child processes built into custom assemblies.", TargetFrameworkMonikers.UapAot)]
        public static void ExitCode_VoidMainAppReturnsSetValue(int mode)
        {
            int expectedExitCode = 123;

            const string AppName = "VoidMainWithExitCodeApp.exe";
            var psi = new ProcessStartInfo();
            if (IsFullFramework || IsNetNative)
            {
                psi.FileName = AppName;
                psi.Arguments = $"{expectedExitCode} {mode}";
            }
            else
            {
                psi.FileName = HostRunner;
                psi.Arguments = $"{AppName} {expectedExitCode} {mode}";
            }

            using (Process p = Process.Start(psi))
            {
                p.WaitForExit();
                Assert.Equal(expectedExitCode, p.ExitCode);
            }
        }
    }
}