forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verilog_linter_configuration_test.cc
895 lines (772 loc) · 30 KB
/
verilog_linter_configuration_test.cc
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "verilog/analysis/verilog_linter_configuration.h"
#include <cstddef>
#include <iosfwd>
#include <map>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "absl/strings/string_view.h"
#include "common/analysis/line_lint_rule.h"
#include "common/analysis/lint_rule_status.h"
#include "common/analysis/syntax_tree_lint_rule.h"
#include "common/analysis/text_structure_lint_rule.h"
#include "common/analysis/token_stream_lint_rule.h"
#include "common/strings/line_column_map.h"
#include "common/text/concrete_syntax_leaf.h"
#include "common/text/concrete_syntax_tree.h"
#include "common/text/syntax_tree_context.h"
#include "common/text/text_structure.h"
#include "common/text/token_info.h"
#include "common/text/tree_builder_test_util.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "verilog/analysis/default_rules.h"
#include "verilog/analysis/descriptions.h"
#include "verilog/analysis/lint_rule_registry.h"
#include "verilog/analysis/verilog_linter.h"
namespace verilog {
namespace {
using analysis::LintRuleDescriptor;
using verible::LineLintRule;
using verible::SyntaxTreeLintRule;
using verible::TextStructureLintRule;
using verible::TextStructureView;
using verible::TokenStreamLintRule;
using testing::IsEmpty;
using testing::SizeIs;
class TestRuleBase : public SyntaxTreeLintRule {
public:
void HandleLeaf(const verible::SyntaxTreeLeaf& leaf,
const verible::SyntaxTreeContext& context) override {}
void HandleNode(const verible::SyntaxTreeNode& node,
const verible::SyntaxTreeContext& context) override {}
verible::LintRuleStatus Report() const override {
return verible::LintRuleStatus();
}
};
class TestRule1 : public TestRuleBase {
public:
using rule_type = SyntaxTreeLintRule;
static const LintRuleDescriptor& GetDescriptor() {
static const LintRuleDescriptor d{
.name = "test-rule-1",
.desc = "TestRule1",
};
return d;
}
};
class TestRule2 : public TestRuleBase {
public:
using rule_type = SyntaxTreeLintRule;
static const LintRuleDescriptor& GetDescriptor() {
static const LintRuleDescriptor d{
.name = "test-rule-2",
.desc = "TestRule2",
};
return d;
}
};
class TestRule3 : public TokenStreamLintRule {
public:
using rule_type = TokenStreamLintRule;
static const LintRuleDescriptor& GetDescriptor() {
static const LintRuleDescriptor d{
.name = "test-rule-3",
.desc = "TestRule3",
};
return d;
}
void HandleToken(const verible::TokenInfo&) override {}
verible::LintRuleStatus Report() const override {
return verible::LintRuleStatus();
}
};
class TestRule4 : public LineLintRule {
public:
using rule_type = LineLintRule;
static const LintRuleDescriptor& GetDescriptor() {
static const LintRuleDescriptor d{
.name = "test-rule-4",
.desc = "TestRule4",
};
return d;
}
void HandleLine(absl::string_view) override {}
verible::LintRuleStatus Report() const override {
return verible::LintRuleStatus();
}
};
class TestRule5 : public TextStructureLintRule {
public:
using rule_type = TextStructureLintRule;
static const LintRuleDescriptor& GetDescriptor() {
static const LintRuleDescriptor d{
.name = "test-rule-5",
.desc = "TestRule5",
};
return d;
}
void Lint(const TextStructureView&, absl::string_view) override {}
verible::LintRuleStatus Report() const override {
return verible::LintRuleStatus();
}
};
VERILOG_REGISTER_LINT_RULE(TestRule1);
VERILOG_REGISTER_LINT_RULE(TestRule2);
VERILOG_REGISTER_LINT_RULE(TestRule3);
VERILOG_REGISTER_LINT_RULE(TestRule4);
VERILOG_REGISTER_LINT_RULE(TestRule5);
// Dummy text structure with a single empty root node for syntax tree.
class FakeTextStructureView : public TextStructureView {
public:
FakeTextStructureView() : TextStructureView("") {
syntax_tree_ = verible::Node();
}
};
// Don't care about line numbers for these tests.
static const verible::LineColumnMap dummy_map("");
// Don't care about file name for these tests.
static const char filename[] = "";
TEST(ProjectPolicyTest, MatchesAnyPath) {
struct TestCase {
ProjectPolicy policy;
absl::string_view filename;
const char* expected_match;
};
const TestCase kTestCases[] = {
{{"policyX", {}, {}, {}, {}, {}}, "filename", nullptr},
{{"policyX", {"file"}, {}, {}, {}, {}}, "filename", "file"},
{{"policyX", {"not-a-match"}, {}, {}, {}, {}}, "filename", nullptr},
{{"policyX", {"xxxx", "yyyy"}, {}, {}, {}, {}}, "file/name.txt", nullptr},
{{"policyX", {"xxxx", "name"}, {}, {}, {}, {}}, "file/name.txt", "name"},
{{"policyX", {"xxxx", "file"}, {}, {}, {}, {}}, "file/name.txt", "file"},
{{"policyX", {"name", "file"}, {}, {}, {}, {}}, "file/name.txt", "name"},
};
for (const auto& test : kTestCases) {
const char* match = test.policy.MatchesAnyPath(test.filename);
if (test.expected_match != nullptr) {
EXPECT_EQ(absl::string_view(match), test.expected_match);
} else {
EXPECT_EQ(match, nullptr);
}
}
}
TEST(ProjectPolicyTest, MatchesAnyExclusions) {
struct TestCase {
ProjectPolicy policy;
absl::string_view filename;
const char* expected_match;
};
const TestCase kTestCases[] = {
{{"policyX", {}, {}, {}, {}, {}}, "filename", nullptr},
{{"policyX", {}, {"file"}, {}, {}, {}}, "filename", "file"},
{{"policyX", {}, {"not-a-match"}, {}, {}, {}}, "filename", nullptr},
{{"policyX", {}, {"xxxx", "yyyy"}, {}, {}, {}}, "file/name.txt", nullptr},
{{"policyX", {}, {"xxxx", "name"}, {}, {}, {}}, "file/name.txt", "name"},
{{"policyX", {}, {"xxxx", "file"}, {}, {}, {}}, "file/name.txt", "file"},
{{"policyX", {}, {"name", "file"}, {}, {}, {}}, "file/name.txt", "name"},
};
for (const auto& test : kTestCases) {
const char* match = test.policy.MatchesAnyExclusions(test.filename);
if (test.expected_match != nullptr) {
EXPECT_EQ(absl::string_view(match), test.expected_match);
} else {
EXPECT_EQ(match, nullptr);
}
}
}
TEST(ProjectPolicyTest, IsValid) {
const std::pair<ProjectPolicy, bool> kTestCases[] = {
{{"policyX", {"path"}, {}, {"owner"}, {"test-rule-1"}, {}}, true},
{{"policyX", {"path"}, {}, {"owner"}, {}, {"test-rule-1"}}, true},
{{"policyX", {"path"}, {}, {"owner"}, {"test-rule-1"}, {"test-rule-2"}},
true},
{{"policyX", {"path"}, {}, {"owner"}, {"not-a-test-rule"}, {}}, false},
{{"policyX", {"path"}, {}, {"owner"}, {}, {"not-a-test-rule"}}, false},
{{"policyX",
{"path"},
{},
{"owner"},
{"test-rule-1", "not-a-test-rule"},
{}},
false},
{{"policyX",
{"path"},
{},
{"owner"},
{},
{"not-a-test-rule", "test-rule-1"}},
false},
};
for (const auto& test : kTestCases) {
EXPECT_EQ(test.first.IsValid(), test.second);
}
}
TEST(ProjectPolicyTest, ListPathGlobs) {
const std::pair<ProjectPolicy, absl::string_view> kTestCases[] = {
{{"policyX", {}, {}, {}, {}, {}}, ""},
{{"policyX", {"path"}, {}, {}, {}, {}}, "*path*"},
{{"policyX", {"path1", "path2"}, {}, {}, {}, {}}, "*path1* | *path2*"},
{{"policyX", {"pa/th1", "pa/th2"}, {}, {}, {}, {}},
"*pa/th1* | *pa/th2*"},
};
for (const auto& test : kTestCases) {
EXPECT_EQ(test.first.ListPathGlobs(), test.second);
}
}
// Confirms that each syntax tree rule yields a set of results.
TEST(VerilogSyntaxTreeLinterConfigurationTest, AddsExpectedNumber) {
LinterConfiguration config;
EXPECT_FALSE(config.RuleIsOn("test-rule-1"));
EXPECT_FALSE(config.RuleIsOn("test-rule-2"));
config.TurnOn("test-rule-1");
EXPECT_TRUE(config.RuleIsOn("test-rule-1"));
EXPECT_FALSE(config.RuleIsOn("test-rule-2"));
config.TurnOn("test-rule-2");
EXPECT_TRUE(config.RuleIsOn("test-rule-1"));
EXPECT_TRUE(config.RuleIsOn("test-rule-2"));
EXPECT_THAT(config.ActiveRuleIds(), SizeIs(2));
VerilogLinter linter;
EXPECT_TRUE(linter.Configure(config, filename).ok());
FakeTextStructureView text_structure;
linter.Lint(text_structure, filename);
auto status = linter.ReportStatus(dummy_map, text_structure.Contents());
EXPECT_THAT(status, SizeIs(2));
}
// Confirms that each token stream rule yields a set of results.
TEST(VerilogTokenStreamLinterConfigurationTest, AddsExpectedNumber) {
LinterConfiguration config;
EXPECT_FALSE(config.RuleIsOn("test-rule-3"));
config.TurnOn("test-rule-3");
EXPECT_TRUE(config.RuleIsOn("test-rule-3"));
EXPECT_THAT(config.ActiveRuleIds(), SizeIs(1));
VerilogLinter linter;
EXPECT_TRUE(linter.Configure(config, filename).ok());
FakeTextStructureView text_structure;
linter.Lint(text_structure, filename);
auto status = linter.ReportStatus(dummy_map, text_structure.Contents());
EXPECT_THAT(status, SizeIs(1));
}
// Confirms that each line-based rule yields a set of results.
TEST(VerilogLineLinterConfigurationTest, AddsExpectedNumber) {
LinterConfiguration config;
EXPECT_FALSE(config.RuleIsOn("test-rule-4"));
config.TurnOn("test-rule-4");
EXPECT_TRUE(config.RuleIsOn("test-rule-4"));
EXPECT_THAT(config.ActiveRuleIds(), SizeIs(1));
VerilogLinter linter;
EXPECT_TRUE(linter.Configure(config, filename).ok());
FakeTextStructureView text_structure;
linter.Lint(text_structure, filename);
auto status = linter.ReportStatus(dummy_map, text_structure.Contents());
EXPECT_THAT(status, SizeIs(1));
}
// Confirms that each text-structure rule yields a set of results.
TEST(VerilogTextStructureLinterConfigurationTest, AddsExpectedNumber) {
LinterConfiguration config;
EXPECT_FALSE(config.RuleIsOn("test-rule-5"));
config.TurnOn("test-rule-5");
EXPECT_TRUE(config.RuleIsOn("test-rule-5"));
EXPECT_THAT(config.ActiveRuleIds(), SizeIs(1));
VerilogLinter linter;
EXPECT_TRUE(linter.Configure(config, filename).ok());
FakeTextStructureView text_structure;
linter.Lint(text_structure, filename);
auto status = linter.ReportStatus(dummy_map, text_structure.Contents());
EXPECT_THAT(status, SizeIs(1));
}
// Verifies that turning on-off rules works.
TEST(VerilogSyntaxTreeLinterConfigurationTest, TurnOnTurnOff) {
LinterConfiguration config;
EXPECT_THAT(config.ActiveRuleIds(), IsEmpty());
config.TurnOn("test-rule-1");
EXPECT_TRUE(config.RuleIsOn("test-rule-1"));
config.TurnOff("test-rule-1");
EXPECT_FALSE(config.RuleIsOn("test-rule-1"));
EXPECT_THAT(config.ActiveRuleIds(), IsEmpty());
VerilogLinter linter;
EXPECT_TRUE(linter.Configure(config, filename).ok());
FakeTextStructureView text_structure;
linter.Lint(text_structure, filename);
auto status = linter.ReportStatus(dummy_map, text_structure.Contents());
EXPECT_THAT(status, IsEmpty());
}
TEST(LinterConfigurationTest, ComparisonOperatorSameElement) {
LinterConfiguration config1, config2;
EXPECT_EQ(config1, config2);
config1.TurnOn("rule-x");
EXPECT_NE(config1, config2);
config2.TurnOn("rule-x");
EXPECT_EQ(config1, config2);
config1.TurnOff("rule-x");
EXPECT_NE(config1, config2);
config2.TurnOff("rule-x");
EXPECT_EQ(config1, config2);
}
TEST(LinterConfigurationTest, ComparisonSameDifferentElement) {
LinterConfiguration config1, config2;
config1.TurnOn("rule-x");
EXPECT_NE(config1, config2);
config2.TurnOn("rule-y");
EXPECT_NE(config1, config2);
config1.TurnOff("rule-x");
EXPECT_NE(config1, config2);
config2.TurnOff("rule-y");
EXPECT_EQ(config1, config2);
}
TEST(LinterConfigurationTest, StreamOperator) {
LinterConfiguration config;
{
std::ostringstream stream;
stream << config;
EXPECT_EQ(stream.str(), "{ }");
}
config.TurnOn("rule-abc");
{
std::ostringstream stream;
stream << config;
EXPECT_EQ(stream.str(), "{ rule-abc }");
}
config.TurnOn("rule-xyz");
{
std::ostringstream stream;
stream << config;
EXPECT_EQ(stream.str(), "{ rule-abc, rule-xyz }");
}
config.TurnOff("rule-abc");
{
std::ostringstream stream;
stream << config;
EXPECT_EQ(stream.str(), "{ rule-xyz }");
}
config.TurnOff("rule-xyz");
{
std::ostringstream stream;
stream << config;
EXPECT_EQ(stream.str(), "{ }");
}
}
TEST(VerilogSyntaxTreeLinterConfigurationTest, DefaultEmpty) {
LinterConfiguration config;
EXPECT_THAT(config.ActiveRuleIds(), IsEmpty());
VerilogLinter linter;
EXPECT_TRUE(linter.Configure(config, filename).ok());
FakeTextStructureView text_structure;
linter.Lint(text_structure, filename);
auto status = linter.ReportStatus(dummy_map, text_structure.Contents());
EXPECT_THAT(status, IsEmpty());
}
TEST(VerilogSyntaxTreeLinterConfigurationTest, UseRuleSetAll) {
LinterConfiguration config;
config.UseRuleSet(RuleSet::kAll);
auto expected_size = analysis::RegisteredSyntaxTreeRulesNames().size() +
analysis::RegisteredTokenStreamRulesNames().size() +
analysis::RegisteredTextStructureRulesNames().size() +
analysis::RegisteredLineRulesNames().size();
EXPECT_THAT(config.ActiveRuleIds(), SizeIs(expected_size));
VerilogLinter linter;
EXPECT_TRUE(linter.Configure(config, filename).ok());
FakeTextStructureView text_structure;
linter.Lint(text_structure, filename);
auto status = linter.ReportStatus(dummy_map, text_structure.Contents());
EXPECT_THAT(status, SizeIs(expected_size));
}
TEST(VerilogSyntaxTreeLinterConfigurationTest, UseRuleSetNone) {
LinterConfiguration config;
config.UseRuleSet(RuleSet::kNone);
EXPECT_THAT(config.ActiveRuleIds(), IsEmpty());
VerilogLinter linter;
EXPECT_TRUE(linter.Configure(config, filename).ok());
FakeTextStructureView text_structure;
linter.Lint(text_structure, filename);
auto status = linter.ReportStatus(dummy_map, text_structure.Contents());
EXPECT_THAT(status, IsEmpty());
}
TEST(VerilogSyntaxTreeLinterConfigurationTest, NoneResets) {
LinterConfiguration config;
config.TurnOn("test-rule-1");
config.TurnOn("test-rule-2");
config.TurnOn("test-rule-3");
config.TurnOn("test-rule-4");
config.UseRuleSet(RuleSet::kNone);
EXPECT_THAT(config.ActiveRuleIds(), IsEmpty());
VerilogLinter linter;
EXPECT_TRUE(linter.Configure(config, filename).ok());
FakeTextStructureView text_structure;
linter.Lint(text_structure, filename);
auto status = linter.ReportStatus(dummy_map, text_structure.Contents());
EXPECT_THAT(status, IsEmpty());
}
TEST(VerilogSyntaxTreeLinterConfigurationTest, UseRuleSetDefault) {
LinterConfiguration config;
config.UseRuleSet(RuleSet::kDefault);
VerilogLinter linter;
EXPECT_TRUE(linter.Configure(config, filename).ok());
FakeTextStructureView text_structure;
linter.Lint(text_structure, filename);
auto status = linter.ReportStatus(dummy_map, text_structure.Contents());
auto expected_size = std::extent<decltype(analysis::kDefaultRuleSet)>::value;
EXPECT_THAT(config.ActiveRuleIds(), SizeIs(expected_size));
EXPECT_THAT(status, SizeIs(expected_size));
}
// Tests that empty policy doesn't cause any change in configuration.
TEST(LinterConfigurationUseProjectPolicyTest, BlankPolicyBlankFilename) {
LinterConfiguration config, default_config;
ProjectPolicy policy;
config.UseProjectPolicy(policy, "");
EXPECT_EQ(config, default_config);
}
// Test single rule can be enabled with path matching.
TEST(LinterConfigurationUseProjectPolicyTest, EnableRule) {
LinterConfiguration config;
ProjectPolicy policy{"policyX", {"path"}, {}, {"owner"}, {}, {"wanted-rule"}};
EXPECT_FALSE(config.RuleIsOn("wanted-rule"));
config.UseProjectPolicy(policy, "some/path/foo");
EXPECT_TRUE(config.RuleIsOn("wanted-rule"));
}
// Test that rule is not enabled because path does not match.
TEST(LinterConfigurationUseProjectPolicyTest, EnableFilePathNotMatched) {
LinterConfiguration config;
ProjectPolicy policy{"policyX", {"not-gonna-match"}, {}, {"owner"},
{}, {"wanted-rule"}};
EXPECT_FALSE(config.RuleIsOn("wanted-rule"));
config.UseProjectPolicy(policy, "some/path/foo");
EXPECT_FALSE(config.RuleIsOn("wanted-rule"));
}
// Test single rule can be disabled with path matching.
TEST(LinterConfigurationUseProjectPolicyTest, DisableRule) {
LinterConfiguration config;
config.TurnOn("unwanted-rule");
ProjectPolicy policy{"policyX", {"path"}, {},
{"owner"}, {"unwanted-rule"}, {}};
EXPECT_TRUE(config.RuleIsOn("unwanted-rule"));
config.UseProjectPolicy(policy, "some/path/foo");
EXPECT_FALSE(config.RuleIsOn("unwanted-rule"));
}
// Test that rule remains enabled because path does not match.
TEST(LinterConfigurationUseProjectPolicyTest, DisableRulePathNotMatched) {
LinterConfiguration config;
config.TurnOn("unwanted-rule");
ProjectPolicy policy{"policyX", {"does-not-match"}, {},
{"owner"}, {"unwanted-rule"}, {}};
EXPECT_TRUE(config.RuleIsOn("unwanted-rule"));
config.UseProjectPolicy(policy, "some/path/foo");
EXPECT_TRUE(config.RuleIsOn("unwanted-rule"));
}
// Test that enabling a rule takes precedence over disabling.
TEST(LinterConfigurationUseProjectPolicyTest, EnableRuleWins) {
LinterConfiguration config;
// Same rule is disabled and enabled.
ProjectPolicy policy{"policyX", {"path"}, {},
{"owner"}, {"wanted-rule"}, {"wanted-rule"}};
EXPECT_FALSE(config.RuleIsOn("wanted-rule"));
config.UseProjectPolicy(policy, "some/path/foo");
EXPECT_TRUE(config.RuleIsOn("wanted-rule"));
}
//
// Tests for parse/unparse on RuleSet
//
TEST(RuleSetTest, ParseRuleSetSuccess) {
std::string none_text = "none";
std::string all_text = "all";
std::string default_text = "default";
std::string none_error = "";
std::string all_error = "";
std::string default_error = "";
RuleSet none_destination, all_destination, default_destination;
bool none_result = AbslParseFlag(none_text, &none_destination, &none_error);
bool all_result = AbslParseFlag(all_text, &all_destination, &all_error);
bool default_result =
AbslParseFlag(default_text, &default_destination, &default_error);
EXPECT_TRUE(none_result);
EXPECT_EQ(none_error, "");
EXPECT_EQ(none_destination, RuleSet::kNone);
EXPECT_TRUE(all_result);
EXPECT_EQ(all_error, "");
EXPECT_EQ(all_destination, RuleSet::kAll);
EXPECT_TRUE(default_result);
EXPECT_EQ(default_error, "");
EXPECT_EQ(default_destination, RuleSet::kDefault);
}
TEST(RuleSetTest, ParseRuleSetError) {
std::string bad_text = "fdsfdfds";
std::string error = "";
RuleSet rule_result;
bool result = AbslParseFlag(bad_text, &rule_result, &error);
EXPECT_FALSE(result);
EXPECT_NE(error, "");
}
TEST(RuleSetTest, UnparseRuleSetSuccess) {
EXPECT_EQ("none", AbslUnparseFlag(RuleSet::kNone));
EXPECT_EQ("default", AbslUnparseFlag(RuleSet::kDefault));
EXPECT_EQ("all", AbslUnparseFlag(RuleSet::kAll));
}
//
// Tests for parse / unparse on RuleBundle
//
TEST(RuleBundleTest, UnparseRuleBundleSeveral) {
RuleBundle bundle = {{{"flag1", {true, ""}}, {"flag2", {true, ""}}}};
std::string expected_comma = "flag2,flag1";
std::string expected_newline = "flag2\nflag1";
std::string result_comma = bundle.UnparseConfiguration(',');
EXPECT_EQ(result_comma, expected_comma);
std::string result_newline = bundle.UnparseConfiguration('\n');
EXPECT_EQ(result_newline, expected_newline);
}
TEST(RuleBundleTest, UnparseRuleBundleSeveralTurnOff) {
RuleBundle bundle = {{{"flag1", {false, ""}}, {"flag2", {true, ""}}}};
std::string expected_comma = "flag2,-flag1";
std::string expected_newline = "flag2\n-flag1";
std::string result_comma = bundle.UnparseConfiguration(',');
EXPECT_EQ(result_comma, expected_comma);
std::string result_newline = bundle.UnparseConfiguration('\n');
EXPECT_EQ(result_newline, expected_newline);
}
TEST(RuleBundleTest, UnparseRuleBundleSeveralConfiguration) {
RuleBundle bundle = {{{"flag1", {false, "foo"}}, {"flag2", {true, "bar"}}}};
std::string expected_comma = "flag2=bar,-flag1=foo";
std::string expected_newline = "flag2=bar\n-flag1=foo";
std::string result_comma = bundle.UnparseConfiguration(',');
EXPECT_EQ(result_comma, expected_comma);
std::string result_newline = bundle.UnparseConfiguration('\n');
EXPECT_EQ(result_newline, expected_newline);
}
TEST(RuleBundleTest, UnparseRuleBundleEmpty) {
RuleBundle bundle = {};
std::string expected = "";
std::string result_comma = bundle.UnparseConfiguration(',');
EXPECT_EQ(result_comma, expected);
std::string result_newline = bundle.UnparseConfiguration('\n');
EXPECT_EQ(result_newline, expected);
}
TEST(RuleBundleTest, ParseRuleBundleEmpty) {
constexpr absl::string_view text = "";
RuleBundle bundle;
std::string error;
bool success = bundle.ParseConfiguration(text, ',', &error);
EXPECT_TRUE(success) << error;
EXPECT_TRUE(error.empty());
EXPECT_TRUE(bundle.rules.empty());
}
TEST(RuleBundleTest, ParseRuleBundleAcceptSeveral) {
// Allow for an optional '+' to enable a rule for symmetry with '-' disable
constexpr absl::string_view text = "test-rule-1,test-rule-2,+test-rule-3";
RuleBundle bundle;
std::string error;
bool success = bundle.ParseConfiguration(text, ',', &error);
ASSERT_TRUE(success) << error;
ASSERT_THAT(bundle.rules, SizeIs(3));
EXPECT_TRUE(error.empty());
EXPECT_TRUE(bundle.rules["test-rule-1"].enabled);
EXPECT_TRUE(bundle.rules["test-rule-2"].enabled);
EXPECT_TRUE(bundle.rules["test-rule-3"].enabled);
}
TEST(RuleBundleTest, ParseRuleBundleAcceptConfiguration) {
constexpr absl::string_view text =
"test-rule-1=foo,test-rule-2=,test-rule-3,-test-rule-4=bar";
RuleBundle bundle;
std::string error;
bool success = bundle.ParseConfiguration(text, ',', &error);
ASSERT_TRUE(success) << error;
ASSERT_THAT(bundle.rules, SizeIs(4));
EXPECT_TRUE(error.empty());
EXPECT_TRUE(bundle.rules["test-rule-1"].enabled);
EXPECT_EQ("foo", bundle.rules["test-rule-1"].configuration);
EXPECT_TRUE(bundle.rules["test-rule-2"].enabled);
EXPECT_TRUE(bundle.rules["test-rule-2"].configuration.empty());
EXPECT_TRUE(bundle.rules["test-rule-3"].enabled);
EXPECT_TRUE(bundle.rules["test-rule-3"].configuration.empty());
EXPECT_FALSE(bundle.rules["test-rule-4"].enabled);
EXPECT_EQ("bar", bundle.rules["test-rule-4"].configuration);
}
TEST(RuleBundleTest, ParseRuleBundleAcceptOne) {
constexpr absl::string_view text = "test-rule-1";
RuleBundle bundle;
std::string error;
bool success = bundle.ParseConfiguration(text, ',', &error);
EXPECT_TRUE(error.empty());
ASSERT_TRUE(success) << error;
ASSERT_THAT(bundle.rules, SizeIs(1));
EXPECT_TRUE(bundle.rules["test-rule-1"].enabled);
}
TEST(RuleBundleTest, ParseRuleWhitespaceAroundAllowed) {
constexpr absl::string_view text =
"\t test-rule-1 \t, +test-rule-2=foo:bar \t";
RuleBundle bundle;
std::string error;
bool success = bundle.ParseConfiguration(text, ',', &error);
EXPECT_TRUE(error.empty());
ASSERT_TRUE(success) << error;
ASSERT_THAT(bundle.rules, SizeIs(2));
EXPECT_TRUE(bundle.rules["test-rule-1"].enabled);
EXPECT_TRUE(bundle.rules["test-rule-2"].enabled);
EXPECT_EQ("foo:bar", bundle.rules["test-rule-2"].configuration);
}
TEST(RuleBundleTest, ParseRuleBundleAcceptSeveralTurnOff) {
constexpr absl::string_view text = "test-rule-1,-test-rule-2";
RuleBundle bundle;
std::string error;
bool success = bundle.ParseConfiguration(text, ',', &error);
ASSERT_TRUE(success) << error;
ASSERT_THAT(bundle.rules, SizeIs(2));
EXPECT_TRUE(error.empty());
EXPECT_TRUE(bundle.rules["test-rule-1"].enabled);
EXPECT_FALSE(bundle.rules["test-rule-2"].enabled);
}
TEST(RuleBundleTest, ParseRuleBundleAcceptOneTurnOff) {
constexpr absl::string_view text = "-test-rule-1";
RuleBundle bundle;
std::string error;
bool success = bundle.ParseConfiguration(text, ',', &error);
ASSERT_TRUE(success) << error;
ASSERT_THAT(bundle.rules, SizeIs(1));
EXPECT_TRUE(error.empty());
EXPECT_FALSE(bundle.rules["test-rule-1"].enabled);
}
TEST(RuleBundleTest, ParseRuleBundleReject) {
constexpr absl::string_view text = "test-rule-1,bad-flag";
RuleBundle bundle;
std::string error;
bool success = bundle.ParseConfiguration(text, ',', &error);
EXPECT_FALSE(success);
EXPECT_EQ(error, "invalid flag \"bad-flag\"");
}
TEST(RuleBundleTest, ParseRuleBundleAcceptMultiline) {
constexpr absl::string_view text = "test-rule-1\n-test-rule-2";
RuleBundle bundle;
std::string error;
bool success = bundle.ParseConfiguration(text, '\n', &error);
ASSERT_TRUE(success) << error;
ASSERT_THAT(bundle.rules, SizeIs(2));
EXPECT_TRUE(error.empty());
EXPECT_TRUE(bundle.rules["test-rule-1"].enabled);
EXPECT_FALSE(bundle.rules["test-rule-2"].enabled);
}
TEST(RuleBundleTest, ParseRuleBundleRejectMultiline) {
constexpr absl::string_view text = "test-rule-1\nbad-flag\n-test-rule-2";
RuleBundle bundle;
std::string error;
bool success = bundle.ParseConfiguration(text, '\n', &error);
EXPECT_FALSE(success);
EXPECT_EQ(error, "invalid flag \"bad-flag\"");
}
TEST(RuleBundleTest, ParseRuleBundleSkipComments) {
constexpr absl::string_view text =
" # some comment after whitespace\n"
"# more comment\n"
"test-rule-1\n"
"-test-rule-2 # some comment\n"
"+test-rule-3=bar:baz # config-comment\n";
{
RuleBundle bundle;
std::string error;
bool success = bundle.ParseConfiguration(text, '\n', &error);
ASSERT_TRUE(success) << error;
ASSERT_THAT(bundle.rules, SizeIs(3));
EXPECT_TRUE(error.empty());
EXPECT_TRUE(bundle.rules["test-rule-1"].enabled);
EXPECT_FALSE(bundle.rules["test-rule-2"].enabled);
EXPECT_TRUE(bundle.rules["test-rule-3"].enabled);
EXPECT_EQ("bar:baz", bundle.rules["test-rule-3"].configuration);
}
}
TEST(RuleBundleTest, ParseRuleBundleIgnoreExtraComma) {
// Multiline rules might still have a comma from the one-line
// rule configuration. They shouldn't harm.
constexpr absl::string_view text =
"test-rule-1,,, \n"
"-test-rule-2=a:b,\n"
"+test-rule-3=bar:baz, # config-comment\n";
{
RuleBundle bundle;
std::string error;
bool success = bundle.ParseConfiguration(text, '\n', &error);
ASSERT_TRUE(success) << error;
ASSERT_NE(error.find(','), std::string::npos) << error; // warning report
ASSERT_THAT(bundle.rules, SizeIs(3));
EXPECT_TRUE(bundle.rules["test-rule-1"].enabled);
EXPECT_FALSE(bundle.rules["test-rule-2"].enabled);
EXPECT_EQ("a:b", bundle.rules["test-rule-2"].configuration);
EXPECT_TRUE(bundle.rules["test-rule-3"].enabled);
EXPECT_EQ("bar:baz", bundle.rules["test-rule-3"].configuration);
}
}
// ConfigureFromOptions Tests
TEST(ConfigureFromOptionsTest, Basic) {
LinterConfiguration config;
LinterOptions options = {.ruleset = RuleSet::kAll,
.rules = RuleBundle(),
.config_file = "",
.rules_config_search = false,
.linting_start_file = "filename",
.waiver_files = "filename"};
auto status = config.ConfigureFromOptions(options);
EXPECT_TRUE(status.ok());
}
TEST(ConfigureFromOptionsTest, LoadFromNonExistingFile) {
LinterConfiguration config;
LinterOptions options = {.ruleset = RuleSet::kAll,
.rules = RuleBundle(),
.config_file = "non-existent-file.txt",
.rules_config_search = false,
.linting_start_file = "filename",
.waiver_files = "filename"};
auto status = config.ConfigureFromOptions(options);
EXPECT_FALSE(status.ok()) << status.message();
}
TEST(ConfigureFromOptionsTest, RulesNumber) {
LinterConfiguration config;
LinterOptions options = {.ruleset = RuleSet::kAll,
.rules = RuleBundle(),
.config_file = "",
.rules_config_search = false,
.linting_start_file = "filename",
.waiver_files = "filename"};
auto status = config.ConfigureFromOptions(options);
EXPECT_TRUE(status.ok());
// Should enable all rules because uses kAll ruleset and an empty rulebundle
auto expected_size = analysis::RegisteredSyntaxTreeRulesNames().size() +
analysis::RegisteredTokenStreamRulesNames().size() +
analysis::RegisteredTextStructureRulesNames().size() +
analysis::RegisteredLineRulesNames().size();
EXPECT_THAT(config.ActiveRuleIds(), SizeIs(expected_size));
}
TEST(ConfigureFromOptionsTest, RulesSelective) {
LinterConfiguration config;
RuleBundle bundle = {
{{analysis::RegisteredSyntaxTreeRulesNames()[0], {false, ""}}}};
LinterOptions options = {.ruleset = RuleSet::kAll,
.rules = bundle,
.config_file = "",
.rules_config_search = false,
.linting_start_file = "filename",
.waiver_files = "filename"};
auto status = config.ConfigureFromOptions(options);
EXPECT_TRUE(status.ok());
// Should enable all rules - 1 because uses kAll ruleset and a rulebundle
// with one rule disabled
auto expected_size = analysis::RegisteredSyntaxTreeRulesNames().size() +
analysis::RegisteredTokenStreamRulesNames().size() +
analysis::RegisteredTextStructureRulesNames().size() +
analysis::RegisteredLineRulesNames().size() - 1;
EXPECT_THAT(config.ActiveRuleIds(), SizeIs(expected_size));
}
// TODO: LinterOptions could be refactored to store the content
// of the configuration files. After this is made it will be possible to
// test the configuration that is applied after reading the files.
} // namespace
} // namespace verilog