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

TestFilter.cs « XUnitWrapperLibrary « Common « tests « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6c4c4f6b57b1b21104e189369261a4965ac8710c (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace XUnitWrapperLibrary;

public class TestFilter
{
    public interface ISearchClause
    {
        bool IsMatch(string fullyQualifiedName, string displayName, string[] traits);
    }

    public enum TermKind
    {
        FullyQualifiedName,
        DisplayName
    }
    public sealed class NameClause : ISearchClause
    {
        public NameClause(TermKind kind, string filter, bool substring)
        {
            Kind = kind;
            Filter = filter;
            Substring = substring;
        }
        public TermKind Kind { get; }
        public string Filter { get; }
        public bool Substring { get; }

        public bool IsMatch(string fullyQualifiedName, string displayName, string[] traits)
        {
            string stringToSearch = Kind switch
            {
                TermKind.FullyQualifiedName => fullyQualifiedName,
                TermKind.DisplayName => displayName,
                _ => throw new InvalidOperationException()
            };

            if (Substring)
            {
                return stringToSearch.Contains(Filter);
            }
            return stringToSearch == Filter;
        }
            
        public override string ToString()
        {
            return $"{Kind}{(Substring ? "~" : "=")}{Filter}";
        }
    }

    public sealed class AndClause : ISearchClause
    {
        private ISearchClause _left;
        private ISearchClause _right;

        public AndClause(ISearchClause left, ISearchClause right)
        {
            _left = left;
            _right = right;
        }

        public bool IsMatch(string fullyQualifiedName, string displayName, string[] traits) => _left.IsMatch(fullyQualifiedName, displayName, traits) && _right.IsMatch(fullyQualifiedName, displayName, traits);
  
        public override string ToString()
        {
            return $"({_left}) && ({_right})";
        }
    }

    public sealed class OrClause : ISearchClause
    {
        private ISearchClause _left;
        private ISearchClause _right;

        public OrClause(ISearchClause left, ISearchClause right)
        {
            _left = left;
            _right = right;
        }

        public bool IsMatch(string fullyQualifiedName, string displayName, string[] traits) => _left.IsMatch(fullyQualifiedName, displayName, traits) || _right.IsMatch(fullyQualifiedName, displayName, traits);
    
        public override string ToString()
        {
            return $"({_left}) || ({_right})";
        }
    }

    public sealed class NotClause : ISearchClause
    {
        private ISearchClause _inner;

        public NotClause(ISearchClause inner)
        {
            _inner = inner;
        }

        public bool IsMatch(string fullyQualifiedName, string displayName, string[] traits) => !_inner.IsMatch(fullyQualifiedName, displayName, traits);
    
        public override string ToString()
        {
            return $"!({_inner})";
        }
    }

    private readonly ISearchClause? _filter;

    // Test exclusion list is a compatibility measure allowing for a smooth migration
    // away from the legacy issues.targets issue tracking system. Before we migrate
    // all tests to the new model, it's easier to keep bug exclusions in the existing
    // issues.targets file as a split model would be very confusing for developers
    // and test monitors.
    private readonly HashSet<string>? _testExclusionList;

    public TestFilter(string? filterString, HashSet<string>? testExclusionList)
    {
        if (filterString is not null)
        {
            if (filterString.IndexOfAny(new[] { '!', '(', ')', '~', '=' }) != -1)
            {
                throw new ArgumentException("Complex test filter expressions are not supported today. The only filters supported today are the simple form supported in 'dotnet test --filter' (substrings of the test's fully qualified name). If further filtering options are desired, file an issue on dotnet/runtime for support.", nameof(filterString));
            }
            _filter = new NameClause(TermKind.FullyQualifiedName, filterString, substring: true);
        }
        _testExclusionList = testExclusionList;
    }

    public TestFilter(ISearchClause? filter, HashSet<string>? testExclusionList)
    {
        _filter = filter;
        _testExclusionList = testExclusionList;
    }

    public bool ShouldRunTest(string fullyQualifiedName, string displayName, string[]? traits = null)
    {
        if (_testExclusionList is not null && _testExclusionList.Contains(displayName))
        {
            return false;
        }
        if (_filter is null)
        {
            return true;
        }
        return _filter.IsMatch(fullyQualifiedName, displayName, traits ?? Array.Empty<string>());
    }
    
    public static HashSet<string> LoadTestExclusionList()
    {
        HashSet<string> output = new ();

        // Try reading the exclusion list as a base64-encoded semicolon-delimited string as a commmand-line arg.
        string[] arguments = Environment.GetCommandLineArgs();
        string? testExclusionListArg = arguments.FirstOrDefault(arg => arg.StartsWith("--exclusion-list="));
        if (testExclusionListArg is not null)
        {
            string testExclusionListPathFromCommandLine = testExclusionListArg.Substring("--exclusion-list=".Length);
            output.UnionWith(File.ReadAllLines(testExclusionListPathFromCommandLine));
        }

        // Try reading the exclusion list as a line-delimited file.
        string? testExclusionListPath = Environment.GetEnvironmentVariable("TestExclusionListPath");
        if (!string.IsNullOrEmpty(testExclusionListPath))
        {
            output.UnionWith(File.ReadAllLines(testExclusionListPath));
        }
        return output;
    }
}