@@ -2,7 +2,7 @@ package codacy.plugins.test
2
2
3
3
import codacy .utils .CollectionHelper
4
4
import com .codacy .plugins .api .PatternDescription
5
- import com .codacy .plugins .api .results .Pattern
5
+ import com .codacy .plugins .api .results .{ Category , Pattern , Tool }
6
6
import com .codacy .plugins .results .traits .DockerToolDocumentation
7
7
import com .codacy .plugins .runners .IDocker
8
8
import com .codacy .plugins .utils .BinaryDockerHelper
@@ -39,70 +39,90 @@ object JsonTests extends ITest {
39
39
}
40
40
}
41
41
42
- (dockerToolDocumentation.toolSpecification, dockerToolDocumentation.patternDescriptions) match {
43
- case (Some (tool), Some (descriptions)) =>
44
- val diffResult = new CollectionHelper [Pattern .Specification , PatternDescription , String ](tool.patterns.toSeq,
45
- descriptions.toSeq)({
46
- pattern =>
47
- val parameters = pattern.parameters.map(_.name.value).toSeq.sorted
48
- generateUniquePatternSignature(pattern.patternId.value, parameters)
49
- }, { description =>
50
- val parameters = description.parameters.getOrElse(Seq .empty).map(_.name.value).toSeq.sorted
51
- generateUniquePatternSignature(description.patternId.value, parameters)
52
- }).fastDiff
53
-
54
- val duplicateDescriptions = descriptions.groupBy(_.patternId).filter { case (_, v) => v.size > 1 }
55
- if (duplicateDescriptions.nonEmpty) {
56
- error(s """
42
+ val onlyAllowedCategories =
43
+ dockerToolDocumentation.toolSpecification.map(validatePatternCategory).getOrElse(true )
44
+
45
+ val noMismatchBetweenPatternsAndDescription =
46
+ (dockerToolDocumentation.toolSpecification, dockerToolDocumentation.patternDescriptions) match {
47
+ case (Some (tool), Some (descriptions)) =>
48
+ val diffResult = new CollectionHelper [Pattern .Specification , PatternDescription , String ](tool.patterns.toSeq,
49
+ descriptions.toSeq)({
50
+ pattern =>
51
+ val parameters = pattern.parameters.map(_.name.value).toSeq.sorted
52
+ generateUniquePatternSignature(pattern.patternId.value, parameters)
53
+ }, { description =>
54
+ val parameters = description.parameters.getOrElse(Seq .empty).map(_.name.value).toSeq.sorted
55
+ generateUniquePatternSignature(description.patternId.value, parameters)
56
+ }).fastDiff
57
+
58
+ val duplicateDescriptions = descriptions.groupBy(_.patternId).filter { case (_, v) => v.size > 1 }
59
+ if (duplicateDescriptions.nonEmpty) {
60
+ error(s """
57
61
|Some patterns were duplicated in /docs/description/description.json
58
62
|
59
63
| * ${duplicateDescriptions.map { case (patternId, _) => patternId }.mkString(" ," )}
60
64
""" .stripMargin)
61
- }
65
+ }
62
66
63
- if (diffResult.newObjects.nonEmpty) {
64
- error(s """
67
+ if (diffResult.newObjects.nonEmpty) {
68
+ error(s """
65
69
|Some patterns were only found in /docs/patterns.json
66
70
|Confirm that all the patterns and parameters present in /docs/patterns.json are also present in /docs/description/description.json
67
71
|
68
72
| * ${diffResult.newObjects.map(_.patternId).mkString(" ," )}
69
73
""" .stripMargin)
70
- }
74
+ }
71
75
72
- if (diffResult.deletedObjects.nonEmpty) {
73
- error(s """
76
+ if (diffResult.deletedObjects.nonEmpty) {
77
+ error(s """
74
78
|Some patterns were only found in /docs/description/description.json
75
79
|Confirm that all the patterns and parameters present in /docs/description/description.json are also present in /docs/patterns.json
76
80
|
77
81
| * ${diffResult.deletedObjects.map(_.patternId).mkString(" ," )}
78
82
""" .stripMargin)
79
- }
83
+ }
80
84
81
- val titlesAboveLimit = descriptions.filter(_.title.length > 255 )
82
- if (titlesAboveLimit.nonEmpty) {
83
- error(s """
85
+ val titlesAboveLimit = descriptions.filter(_.title.length > 255 )
86
+ if (titlesAboveLimit.nonEmpty) {
87
+ error(s """
84
88
|Some titles are too big in /docs/description/description.json
85
89
|The max size of a title is 255 characters
86
90
|
87
91
| * ${titlesAboveLimit.map(_.patternId).mkString(" , " )}
88
92
""" .stripMargin)
89
- }
93
+ }
90
94
91
- val descriptionsAboveLimit = descriptions.filter(_.description.getOrElse(" " ).length > 500 )
92
- if (descriptionsAboveLimit.nonEmpty) {
93
- error(s """
95
+ val descriptionsAboveLimit = descriptions.filter(_.description.getOrElse(" " ).length > 500 )
96
+ if (descriptionsAboveLimit.nonEmpty) {
97
+ error(s """
94
98
|Some descriptions are too big in /docs/description/description.json
95
99
|The max size of a description is 500 characters
96
100
|
97
101
| * ${descriptionsAboveLimit.map(_.patternId).mkString(" , " )}
98
102
""" .stripMargin)
99
- }
103
+ }
100
104
101
- ignoreDescriptions ||
102
- (diffResult.newObjects.isEmpty && diffResult.deletedObjects.isEmpty)
105
+ ignoreDescriptions ||
106
+ (diffResult.newObjects.isEmpty && diffResult.deletedObjects.isEmpty)
103
107
104
- case _ => ignoreDescriptions
105
- }
108
+ case _ => ignoreDescriptions
109
+ }
110
+
111
+ onlyAllowedCategories && noMismatchBetweenPatternsAndDescription
112
+ }
113
+
114
+ // TS-682: Validate disabled categories aren't used
115
+ private def validatePatternCategory (toolSpec : Tool .Specification ): Boolean = {
116
+ toolSpec.patterns.forall(pattern => {
117
+ val isAllowed = Category .allowedCategories.contains(pattern.category)
118
+ if (! isAllowed) {
119
+ error(s """
120
+ | Disabled category " ${pattern.category}" used by pattern " ${pattern.patternId}".
121
+ | Allowed categories are: ${Category .allowedCategories.mkString(" ," )}.
122
+ """ .stripMargin)
123
+ }
124
+ isAllowed
125
+ })
106
126
}
107
127
108
128
private def readDockerToolDocumentation (dockerImage : DockerImage ) = {
0 commit comments