Skip to content

Commit 0af7d4c

Browse files
jameskleehgraemerocher
authored andcommitted
Issue #10034. Show warning if feature doesn't exist (#10053)
* Show warning when creating an application with a specified feature that doesn't exist * cleanup
1 parent 549316c commit 0af7d4c

File tree

2 files changed

+125
-3
lines changed

2 files changed

+125
-3
lines changed

grails-shell/src/main/groovy/org/grails/cli/profile/commands/CreateAppCommand.groovy

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,22 @@ class CreateAppCommand extends ArgumentCompletingCommand implements ProfileRepos
314314
}
315315

316316
protected Iterable<Feature> evaluateFeatures(Profile profile, CommandLine commandLine) {
317-
def requestedFeatures = commandLine.optionValue("features")?.toString()?.split(',')
317+
String[] requestedFeatures = commandLine.optionValue("features")?.toString()?.split(',')
318318
if(requestedFeatures) {
319-
def featureNames = Arrays.asList(requestedFeatures)
320-
return (profile.features.findAll() { Feature f -> featureNames.contains(f.name)} + profile.requiredFeatures).unique()
319+
List<String> requestedFeaturesList = requestedFeatures.toList()
320+
List<String> allFeatureNames = profile.features*.name
321+
List<String> validFeatureNames = requestedFeaturesList.intersect(allFeatureNames)
322+
requestedFeaturesList.removeAll(allFeatureNames)
323+
requestedFeaturesList.each { String invalidFeature ->
324+
List possibleSolutions = allFeatureNames.findAll { it.substring(0, 2) == invalidFeature.substring(0, 2) }
325+
StringBuilder warning = new StringBuilder("Feature ${invalidFeature} does not exist in the profile ${profile.name}!")
326+
if (possibleSolutions) {
327+
warning.append(" Possible solutions: ")
328+
warning.append(possibleSolutions.join(", "))
329+
}
330+
GrailsConsole.getInstance().warn(warning.toString())
331+
}
332+
return (profile.features.findAll() { Feature f -> validFeatureNames.contains(f.name) } + profile.requiredFeatures).unique()
321333
}
322334
else {
323335
return (profile.defaultFeatures + profile.requiredFeatures).unique()
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package org.grails.cli.profile.commands
2+
3+
import grails.build.logging.GrailsConsole
4+
import org.grails.build.parsing.CommandLine
5+
import org.grails.build.parsing.DefaultCommandLine
6+
import org.grails.cli.profile.Feature
7+
import org.grails.cli.profile.Profile
8+
import org.spockframework.util.StringMessagePrintStream
9+
import spock.lang.Shared
10+
import spock.lang.Specification
11+
12+
/**
13+
* Created by Jim on 7/18/2016.
14+
*/
15+
class CreateAppCommandSpec extends Specification {
16+
17+
@Shared
18+
StringPrintStream sps
19+
20+
PrintStream originalOut
21+
22+
void setup() {
23+
originalOut = GrailsConsole.instance.out
24+
sps = new StringPrintStream()
25+
GrailsConsole.instance.out = sps
26+
}
27+
28+
void cleanup() {
29+
GrailsConsole.instance.out = originalOut
30+
}
31+
32+
void "test evaluateFeatures - multiple, some valid"() {
33+
given:
34+
Feature bar = Mock(Feature) {
35+
2 * getName() >> "bar"
36+
}
37+
Profile profile = Mock(Profile) {
38+
1 * getName() >> "web"
39+
2 * getFeatures() >> [bar]
40+
1 * getRequiredFeatures() >> []
41+
}
42+
CommandLine commandLine = new DefaultCommandLine().parseNew(["create-app", "foo", "-features=foo,bar"] as String[])
43+
44+
when:
45+
Iterable<Feature> features = new CreateAppCommand().evaluateFeatures(profile, commandLine)
46+
47+
then:
48+
features.size() == 1
49+
features[0] == bar
50+
sps.toString() == "Warning |\nFeature foo does not exist in the profile web!\n"
51+
}
52+
53+
void "test evaluateFeatures - multiple, all valid"() {
54+
given:
55+
Feature foo = Mock(Feature) {
56+
2 * getName() >> "foo"
57+
}
58+
Feature bar = Mock(Feature) {
59+
2 * getName() >> "bar"
60+
}
61+
Profile profile = Mock(Profile) {
62+
0 * getName()
63+
2 * getFeatures() >> [foo, bar]
64+
1 * getRequiredFeatures() >> []
65+
}
66+
CommandLine commandLine = new DefaultCommandLine().parseNew(["create-app", "foo", "-features=foo,bar"] as String[])
67+
68+
when:
69+
Iterable<Feature> features = new CreateAppCommand().evaluateFeatures(profile, commandLine)
70+
71+
then:
72+
features.size() == 2
73+
features[0] == foo
74+
features[1] == bar
75+
sps.toString() == ""
76+
}
77+
78+
void "test evaluateFeatures fat finger"() {
79+
given:
80+
Feature bar = Mock(Feature) {
81+
2 * getName() >> "mongodb"
82+
}
83+
Profile profile = Mock(Profile) {
84+
1 * getName() >> "web"
85+
2 * getFeatures() >> [bar]
86+
1 * getRequiredFeatures() >> []
87+
}
88+
CommandLine commandLine = new DefaultCommandLine().parseNew(["create-app", "foo", "-features=mongo"] as String[])
89+
90+
when:
91+
Iterable<Feature> features = new CreateAppCommand().evaluateFeatures(profile, commandLine)
92+
93+
94+
then:
95+
features.size() == 0
96+
sps.toString() == "Warning |\nFeature mongo does not exist in the profile web! Possible solutions: mongodb\n"
97+
}
98+
99+
class StringPrintStream extends StringMessagePrintStream {
100+
StringBuilder stringBuilder = new StringBuilder()
101+
@Override
102+
protected void printed(String message) {
103+
stringBuilder.append(message)
104+
}
105+
106+
String toString() {
107+
stringBuilder.toString()
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)