Skip to content

Commit f643181

Browse files
fix: detect project folder for gradle kotlin projects (#19614) (#19615)
Fixes #18682 Co-authored-by: Marco Collovati <marco@vaadin.com>
1 parent 61459f5 commit f643181

2 files changed

Lines changed: 105 additions & 3 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/AbstractConfiguration.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ default File getProjectFolder() {
230230
*/
231231
String baseDirCandidate = System.getProperty("user.dir", ".");
232232
Path path = Paths.get(baseDirCandidate);
233-
if (path.toFile().isDirectory()
234-
&& (path.resolve("pom.xml").toFile().exists()
235-
|| path.resolve("build.gradle").toFile().exists())) {
233+
if (path.toFile().isDirectory() && (path.resolve("pom.xml").toFile()
234+
.exists() || path.resolve("build.gradle").toFile().exists()
235+
|| path.resolve("build.gradle.kts").toFile().exists())) {
236236
return path.toAbsolutePath().toFile();
237237
} else {
238238
throw new IllegalStateException(String.format(
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2000-2024 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.vaadin.flow.server;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
import org.junit.Assert;
23+
import org.junit.Rule;
24+
import org.junit.Test;
25+
import org.junit.rules.TemporaryFolder;
26+
27+
public class AbstractConfigurationTest {
28+
29+
@Rule
30+
public TemporaryFolder temporaryFolder = new TemporaryFolder();
31+
32+
AbstractConfiguration configuration = new AbstractConfiguration() {
33+
@Override
34+
public boolean isProductionMode() {
35+
return false;
36+
}
37+
38+
@Override
39+
public String getStringProperty(String name, String defaultValue) {
40+
return null;
41+
}
42+
43+
@Override
44+
public boolean getBooleanProperty(String name, boolean defaultValue) {
45+
return false;
46+
}
47+
};
48+
49+
@Test
50+
public void getProjectFolder_mavenProject_detected() throws IOException {
51+
assertProjectFolderDetected("pom.xml");
52+
}
53+
54+
@Test
55+
public void getProjectFolder_gradleProject_detected() throws IOException {
56+
assertProjectFolderDetected("build.gradle");
57+
}
58+
59+
@Test
60+
public void getProjectFolder_gradleKotlinProject_detected()
61+
throws IOException {
62+
assertProjectFolderDetected("build.gradle.kts");
63+
}
64+
65+
@Test
66+
public void getProjectFolder_unknownProject_throws() throws IOException {
67+
withTemporaryUserDir(() -> {
68+
IllegalStateException exception = Assert.assertThrows(
69+
IllegalStateException.class,
70+
configuration::getProjectFolder);
71+
Assert.assertTrue(exception.getMessage().contains(
72+
"Failed to determine project directory for dev mode"));
73+
Assert.assertTrue(exception.getMessage()
74+
.contains(temporaryFolder.getRoot().getAbsolutePath()));
75+
});
76+
}
77+
78+
private void assertProjectFolderDetected(String projectFile)
79+
throws IOException {
80+
temporaryFolder.newFile(projectFile);
81+
withTemporaryUserDir(() -> {
82+
File projectFolder = configuration.getProjectFolder();
83+
Assert.assertEquals(temporaryFolder.getRoot(), projectFolder);
84+
});
85+
}
86+
87+
private void withTemporaryUserDir(Runnable test) throws IOException {
88+
String userDir = System.getProperty("user.dir");
89+
try {
90+
System.setProperty("user.dir",
91+
temporaryFolder.getRoot().getAbsolutePath());
92+
test.run();
93+
} finally {
94+
if (userDir != null) {
95+
System.setProperty("user.dir", userDir);
96+
} else {
97+
System.clearProperty("user.dir");
98+
}
99+
}
100+
}
101+
102+
}

0 commit comments

Comments
 (0)