|
| 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