@@ -70,7 +70,6 @@ public void apply(Project project) {
70
70
project .getRootProject ().getPluginManager ().apply (DockerSupportPlugin .class );
71
71
72
72
TaskContainer tasks = project .getTasks ();
73
- TestFixtureExtension extension = project .getExtensions ().create ("testFixtures" , TestFixtureExtension .class , project );
74
73
Provider <DockerComposeThrottle > dockerComposeThrottle = project .getGradle ()
75
74
.getSharedServices ()
76
75
.registerIfAbsent (DOCKER_COMPOSE_THROTTLE , DockerComposeThrottle .class , spec -> spec .getMaxParallelUsages ().set (1 ));
@@ -84,73 +83,63 @@ public void apply(Project project) {
84
83
File testFixturesDir = project .file ("testfixtures_shared" );
85
84
ext .set ("testFixturesDir" , testFixturesDir );
86
85
87
- if (project .file (DOCKER_COMPOSE_YML ).exists ()) {
88
- project .getPluginManager ().apply (BasePlugin .class );
89
- project .getPluginManager ().apply (DockerComposePlugin .class );
90
- TaskProvider <TestFixtureTask > preProcessFixture = project .getTasks ().register ("preProcessFixture" , TestFixtureTask .class , t -> {
91
- t .getFixturesDir ().set (testFixturesDir );
92
- t .doFirst (task -> {
93
- try {
94
- Files .createDirectories (testFixturesDir .toPath ());
95
- } catch (IOException e ) {
96
- throw new UncheckedIOException (e );
97
- }
98
- });
99
- });
100
- TaskProvider <Task > buildFixture = project .getTasks ()
101
- .register ("buildFixture" , t -> t .dependsOn (preProcessFixture , tasks .named ("composeUp" )));
102
-
103
- TaskProvider <TestFixtureTask > postProcessFixture = project .getTasks ()
104
- .register ("postProcessFixture" , TestFixtureTask .class , task -> {
105
- task .getFixturesDir ().set (testFixturesDir );
106
- task .dependsOn (buildFixture );
107
- configureServiceInfoForTask (
108
- task ,
109
- project ,
110
- false ,
111
- (name , port ) -> task .getExtensions ().getByType (ExtraPropertiesExtension .class ).set (name , port )
112
- );
113
- });
114
-
115
- maybeSkipTask (dockerSupport , preProcessFixture );
116
- maybeSkipTask (dockerSupport , postProcessFixture );
117
- maybeSkipTask (dockerSupport , buildFixture );
118
-
119
- ComposeExtension composeExtension = project .getExtensions ().getByType (ComposeExtension .class );
120
- composeExtension .setProjectName (project .getName ());
121
- composeExtension .getUseComposeFiles ().addAll (Collections .singletonList (DOCKER_COMPOSE_YML ));
122
- composeExtension .getRemoveContainers ().set (true );
123
- composeExtension .getCaptureContainersOutput ()
124
- .set (EnumSet .of (LogLevel .INFO , LogLevel .DEBUG ).contains (project .getGradle ().getStartParameter ().getLogLevel ()));
125
- composeExtension .getUseDockerComposeV2 ().set (false );
126
- composeExtension .getExecutable ().set (this .providerFactory .provider (() -> {
127
- String composePath = dockerSupport .get ().getDockerAvailability ().dockerComposePath ();
128
- LOGGER .debug ("Docker Compose path: {}" , composePath );
129
- return composePath != null ? composePath : "/usr/bin/docker-compose" ;
130
- }));
131
-
132
- tasks .named ("composeUp" ).configure (t -> {
133
- // Avoid running docker-compose tasks in parallel in CI due to some issues on certain Linux distributions
134
- if (BuildParams .isCi ()) {
135
- t .usesService (dockerComposeThrottle );
86
+ if (project .file (DOCKER_COMPOSE_YML ).exists () == false ) {
87
+ // if only one fixture is used, that's this one, but without a compose file that's not a valid configuration
88
+ throw new IllegalStateException ("No " + DOCKER_COMPOSE_YML + " found for " + project .getPath () + "." );
89
+ }
90
+ project .getPluginManager ().apply (BasePlugin .class );
91
+ project .getPluginManager ().apply (DockerComposePlugin .class );
92
+ TaskProvider <TestFixtureTask > preProcessFixture = project .getTasks ().register ("preProcessFixture" , TestFixtureTask .class , t -> {
93
+ t .getFixturesDir ().set (testFixturesDir );
94
+ t .doFirst (task -> {
95
+ try {
96
+ Files .createDirectories (testFixturesDir .toPath ());
97
+ } catch (IOException e ) {
98
+ throw new UncheckedIOException (e );
136
99
}
137
- t .mustRunAfter (preProcessFixture );
138
100
});
139
- tasks .named ("composePull" ).configure (t -> t .mustRunAfter (preProcessFixture ));
140
- tasks .named ("composeDown" ).configure (t -> t .doLast (t2 -> getFileSystemOperations ().delete (d -> d .delete (testFixturesDir ))));
141
- } else {
142
- project .afterEvaluate (spec -> {
143
- if (extension .fixtures .isEmpty ()) {
144
- // if only one fixture is used, that's this one, but without a compose file that's not a valid configuration
145
- throw new IllegalStateException (
146
- "No " + DOCKER_COMPOSE_YML + " found for " + project .getPath () + " nor does it use other fixtures."
147
- );
148
- }
101
+ });
102
+ TaskProvider <Task > buildFixture = project .getTasks ()
103
+ .register ("buildFixture" , t -> t .dependsOn (preProcessFixture , tasks .named ("composeUp" )));
104
+
105
+ TaskProvider <TestFixtureTask > postProcessFixture = project .getTasks ()
106
+ .register ("postProcessFixture" , TestFixtureTask .class , task -> {
107
+ task .getFixturesDir ().set (testFixturesDir );
108
+ task .dependsOn (buildFixture );
109
+ configureServiceInfoForTask (
110
+ task ,
111
+ project ,
112
+ false ,
113
+ (name , port ) -> task .getExtensions ().getByType (ExtraPropertiesExtension .class ).set (name , port )
114
+ );
149
115
});
150
- }
151
116
152
- extension .fixtures .matching (fixtureProject -> fixtureProject .equals (project ) == false )
153
- .all (fixtureProject -> project .evaluationDependsOn (fixtureProject .getPath ()));
117
+ maybeSkipTask (dockerSupport , preProcessFixture );
118
+ maybeSkipTask (dockerSupport , postProcessFixture );
119
+ maybeSkipTask (dockerSupport , buildFixture );
120
+
121
+ ComposeExtension composeExtension = project .getExtensions ().getByType (ComposeExtension .class );
122
+ composeExtension .setProjectName (project .getName ());
123
+ composeExtension .getUseComposeFiles ().addAll (Collections .singletonList (DOCKER_COMPOSE_YML ));
124
+ composeExtension .getRemoveContainers ().set (true );
125
+ composeExtension .getCaptureContainersOutput ()
126
+ .set (EnumSet .of (LogLevel .INFO , LogLevel .DEBUG ).contains (project .getGradle ().getStartParameter ().getLogLevel ()));
127
+ composeExtension .getUseDockerComposeV2 ().set (false );
128
+ composeExtension .getExecutable ().set (this .providerFactory .provider (() -> {
129
+ String composePath = dockerSupport .get ().getDockerAvailability ().dockerComposePath ();
130
+ LOGGER .debug ("Docker Compose path: {}" , composePath );
131
+ return composePath != null ? composePath : "/usr/bin/docker-compose" ;
132
+ }));
133
+
134
+ tasks .named ("composeUp" ).configure (t -> {
135
+ // Avoid running docker-compose tasks in parallel in CI due to some issues on certain Linux distributions
136
+ if (BuildParams .isCi ()) {
137
+ t .usesService (dockerComposeThrottle );
138
+ }
139
+ t .mustRunAfter (preProcessFixture );
140
+ });
141
+ tasks .named ("composePull" ).configure (t -> t .mustRunAfter (preProcessFixture ));
142
+ tasks .named ("composeDown" ).configure (t -> t .doLast (t2 -> getFileSystemOperations ().delete (d -> d .delete (testFixturesDir ))));
154
143
155
144
// Skip docker compose tasks if it is unavailable
156
145
maybeSkipTasks (tasks , dockerSupport , Test .class );
@@ -161,17 +150,18 @@ public void apply(Project project) {
161
150
maybeSkipTasks (tasks , dockerSupport , ComposePull .class );
162
151
maybeSkipTasks (tasks , dockerSupport , ComposeDown .class );
163
152
164
- tasks .withType (Test .class ).configureEach (task -> extension . fixtures . all ( fixtureProject -> {
165
- task .dependsOn (fixtureProject . getTasks (). named ( " postProcessFixture" ) );
166
- task .finalizedBy (fixtureProject . getTasks () .named ("composeDown" ));
153
+ tasks .withType (Test .class ).configureEach (testTask -> {
154
+ testTask .dependsOn (postProcessFixture );
155
+ testTask .finalizedBy (tasks .named ("composeDown" ));
167
156
configureServiceInfoForTask (
168
- task ,
169
- fixtureProject ,
157
+ testTask ,
158
+ project ,
170
159
true ,
171
- (name , host ) -> task .getExtensions ().getByType (SystemPropertyCommandLineArgumentProvider .class ).systemProperty (name , host )
160
+ (name , host ) -> testTask .getExtensions ()
161
+ .getByType (SystemPropertyCommandLineArgumentProvider .class )
162
+ .systemProperty (name , host )
172
163
);
173
- }));
174
-
164
+ });
175
165
}
176
166
177
167
private void maybeSkipTasks (TaskContainer tasks , Provider <DockerSupportService > dockerSupport , Class <? extends DefaultTask > taskClass ) {
@@ -203,28 +193,20 @@ private void configureServiceInfoForTask(
203
193
task .doFirst (new Action <Task >() {
204
194
@ Override
205
195
public void execute (Task theTask ) {
206
- TestFixtureExtension extension = theTask .getProject ().getExtensions ().getByType (TestFixtureExtension .class );
207
-
208
- fixtureProject .getExtensions ()
209
- .getByType (ComposeExtension .class )
210
- .getServicesInfos ()
211
- .entrySet ()
212
- .stream ()
213
- .filter (entry -> enableFilter == false || extension .isServiceRequired (entry .getKey (), fixtureProject .getPath ()))
214
- .forEach (entry -> {
215
- String service = entry .getKey ();
216
- ServiceInfo infos = entry .getValue ();
217
- infos .getTcpPorts ().forEach ((container , host ) -> {
218
- String name = "test.fixtures." + service + ".tcp." + container ;
219
- theTask .getLogger ().info ("port mapping property: {}={}" , name , host );
220
- consumer .accept (name , host );
221
- });
222
- infos .getUdpPorts ().forEach ((container , host ) -> {
223
- String name = "test.fixtures." + service + ".udp." + container ;
224
- theTask .getLogger ().info ("port mapping property: {}={}" , name , host );
225
- consumer .accept (name , host );
226
- });
196
+ fixtureProject .getExtensions ().getByType (ComposeExtension .class ).getServicesInfos ().entrySet ().stream ().forEach (entry -> {
197
+ String service = entry .getKey ();
198
+ ServiceInfo infos = entry .getValue ();
199
+ infos .getTcpPorts ().forEach ((container , host ) -> {
200
+ String name = "test.fixtures." + service + ".tcp." + container ;
201
+ theTask .getLogger ().info ("port mapping property: {}={}" , name , host );
202
+ consumer .accept (name , host );
227
203
});
204
+ infos .getUdpPorts ().forEach ((container , host ) -> {
205
+ String name = "test.fixtures." + service + ".udp." + container ;
206
+ theTask .getLogger ().info ("port mapping property: {}={}" , name , host );
207
+ consumer .accept (name , host );
208
+ });
209
+ });
228
210
}
229
211
});
230
212
}
0 commit comments