Skip to content

Commit 7ddd2f5

Browse files
committed
tests: use FileProvider API to access file paths
1 parent 23c30ee commit 7ddd2f5

File tree

22 files changed

+202
-87
lines changed

22 files changed

+202
-87
lines changed

.androidide_root

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
DO NOT DELETE!
2+
3+
This file is used in tests to check for the project's root directory.
Binary file not shown.

lsp/testing/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ dependencies {
3333
implementation(projects.common)
3434
implementation(projects.eventbusAndroid)
3535
implementation(projects.subprojects.toolingApiTesting)
36+
implementation(projects.shared)
37+
3638
implementation(libs.common.editor)
3739
implementation(libs.tests.junit)
3840
implementation(libs.tests.google.truth)
3941
implementation(libs.tests.robolectric)
4042
implementation(libs.tests.mockk)
41-
implementation(projects.actions)
4243
}

lsp/testing/src/main/java/com/itsaky/androidide/lsp/api/LSPTest.kt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,20 @@ import com.itsaky.androidide.tooling.api.messages.InitializeProjectMessage
3939
import com.itsaky.androidide.tooling.testing.ToolingApiTestLauncher
4040
import com.itsaky.androidide.tooling.testing.ToolingApiTestLauncher.MultiVersionTestClient
4141
import com.itsaky.androidide.utils.Environment
42+
import com.itsaky.androidide.utils.FileProvider
4243
import com.itsaky.androidide.utils.ILogger
4344
import io.github.rosemoe.sora.text.Content
4445
import io.mockk.every
4546
import io.mockk.mockkStatic
46-
import java.io.File
47-
import java.nio.file.Path
48-
import kotlin.io.path.pathString
4947
import org.greenrobot.eventbus.EventBus
5048
import org.junit.Before
5149
import org.junit.runner.RunWith
5250
import org.robolectric.RobolectricTestRunner
5351
import org.robolectric.RuntimeEnvironment
5452
import org.robolectric.annotation.Config
53+
import java.io.File
54+
import java.nio.file.Path
55+
import kotlin.io.path.pathString
5556

5657
/**
5758
* Runs tests for a language server.
@@ -87,7 +88,6 @@ abstract class LSPTest {
8788
val (server, project) =
8889
ToolingApiTestLauncher()
8990
.launchServer(
90-
implDir = FileProvider.implModule().pathString,
9191
client = MultiVersionTestClient()
9292
)
9393
this.toolingProject = project
@@ -96,7 +96,7 @@ abstract class LSPTest {
9696
Lookup.DEFAULT.update(BuildService.KEY_PROJECT_PROXY, project)
9797

9898
server
99-
.initialize(InitializeProjectMessage(FileProvider.projectRoot().toFile().absolutePath))
99+
.initialize(InitializeProjectMessage(FileProvider.testProjectRoot().toFile().absolutePath))
100100
.get()
101101

102102
Environment.ANDROID_JAR = FileProvider.resources().resolve("android.jar").toFile()
@@ -130,7 +130,17 @@ abstract class LSPTest {
130130

131131
// As the content has been changed, we have to
132132
// Update the content in language server
133-
dispatchEvent(DocumentChangeEvent(file!!, contents.toString(), contents.toString(), 1, DELETE, 0, Range.NONE))
133+
dispatchEvent(
134+
DocumentChangeEvent(
135+
file!!,
136+
contents.toString(),
137+
contents.toString(),
138+
1,
139+
DELETE,
140+
0,
141+
Range.NONE
142+
)
143+
)
134144
}
135145

136146
@JvmOverloads

shared/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ dependencies {
2525
implementation(libs.androidx.annotation)
2626
implementation(libs.google.guava)
2727
implementation(projects.logger)
28+
29+
testImplementation(libs.tests.google.truth)
30+
testImplementation(libs.tests.junit)
2831
}

lsp/testing/src/main/java/com/itsaky/androidide/lsp/api/FileProvider.kt renamed to shared/src/main/java/com/itsaky/androidide/utils/FileProvider.kt

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,32 @@
1414
* You should have received a copy of the GNU General Public License
1515
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
1616
*/
17-
package com.itsaky.androidide.lsp.api
17+
package com.itsaky.androidide.utils
1818

1919
import java.io.File
2020
import java.nio.file.Path
2121
import java.nio.file.Paths
22+
import kotlin.io.path.ExperimentalPathApi
23+
import kotlin.io.path.absolute
24+
import kotlin.io.path.name
2225
import kotlin.io.path.readText
26+
import kotlin.io.path.walk
27+
28+
const val PROJECT_ROOT_FILE = ".androidide_root"
2329

2430
/**
25-
* Provides paths to source files in 'resources' directory.
31+
* Provides paths to various directories and files.
2632
*
2733
* @author Akash Yadav
2834
*/
2935
class FileProvider {
3036

3137
companion object {
3238

39+
private val projectRoot by lazy {
40+
currentDir().findProjectRoot()?.absolute()?.normalize()
41+
}
42+
3343
@JvmField
3444
val extension = run {
3545
val file = File(".").canonicalFile
@@ -41,8 +51,15 @@ class FileProvider {
4151

4252
@JvmStatic fun currentDir(): Path = Paths.get(System.getProperty("user.dir")!!)
4353

44-
@JvmStatic fun implModule(): Path = currentDir().resolve("../../subprojects/tooling-api-impl")
45-
@JvmStatic fun projectRoot(): Path = currentDir().resolve("../../tests/test-project")
54+
@JvmStatic fun implModule(): Path = projectRoot().resolve("subprojects/tooling-api-impl")
55+
56+
@JvmStatic
57+
fun projectRoot(): Path =
58+
checkNotNull(projectRoot) {
59+
"Unable to file project root. Check if '${PROJECT_ROOT_FILE}' file exists in the project root directory."
60+
}
61+
62+
@JvmStatic fun testProjectRoot(): Path = projectRoot().resolve("tests/test-project")
4663

4764
/**
4865
* Get the path to the 'resources' directory.
@@ -51,7 +68,7 @@ class FileProvider {
5168
*/
5269
@JvmStatic
5370
fun resources(): Path {
54-
return projectRoot().resolve("app/src/main/resources")
71+
return testProjectRoot().resolve("app/src/main/resources")
5572
}
5673

5774
/**
@@ -68,3 +85,12 @@ class FileProvider {
6885
@JvmStatic fun contents(file: Path): StringBuilder = StringBuilder(file.readText())
6986
}
7087
}
88+
89+
@OptIn(ExperimentalPathApi::class)
90+
private fun Path.findProjectRoot(): Path? {
91+
if (walk().find { it.name == PROJECT_ROOT_FILE } != null) {
92+
return this
93+
}
94+
95+
return parent?.findProjectRoot()
96+
}

lsp/testing/src/test/java/com/itsaky/androidide/lsp/api/FileProviderTest.kt renamed to shared/src/test/java/com/itsaky/androidide/utils/FileProviderTest.kt

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,40 @@
1515
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
1616
*/
1717

18-
package com.itsaky.androidide.lsp.api
18+
package com.itsaky.androidide.utils
1919

20-
import com.google.common.truth.Truth
20+
import com.google.common.truth.Truth.assertThat
21+
import org.junit.Test
2122
import java.io.File
2223
import java.nio.file.Files
23-
import org.junit.Test
24-
import org.junit.runner.RunWith
25-
import org.robolectric.RobolectricTestRunner
26-
import org.robolectric.annotation.Config
2724

28-
/** @author Akash Yadav */
29-
@RunWith(RobolectricTestRunner::class)
30-
@Config(manifest = Config.NONE)
3125
class FileProviderTest {
3226

3327
@Test
3428
fun testPath() {
3529
val path = FileProvider.sourceFile("SourceFileTest")
36-
Truth.assertThat(Files.exists(path)).isTrue()
37-
Truth.assertThat(path.fileName.toString()).isEqualTo("SourceFileTest_template.java")
30+
assertThat(Files.exists(path)).isTrue()
31+
assertThat(path.fileName.toString()).isEqualTo("SourceFileTest_template.java")
3832
}
3933

4034
@Test
4135
fun testNested() {
4236
val path = FileProvider.sourceFile("package/SourceFileTest")
43-
Truth.assertThat(Files.exists(path)).isTrue()
44-
Truth.assertThat(path.fileName.toString()).isEqualTo("SourceFileTest_template.java")
37+
assertThat(Files.exists(path)).isTrue()
38+
assertThat(path.fileName.toString()).isEqualTo("SourceFileTest_template.java")
4539
}
4640

4741
@Test
4842
fun testExtension() {
4943
val folder = File(".").canonicalFile
5044

51-
Truth.assertThat(FileProvider.extension).isNotEmpty()
52-
Truth.assertThat(FileProvider.extension)
45+
assertThat(FileProvider.extension).isNotEmpty()
46+
assertThat(FileProvider.extension)
5347
.isEqualTo(
5448
when (folder.name) {
5549
"xml" -> "xml"
5650
else -> "java"
5751
}
5852
)
5953
}
60-
}
54+
}

subprojects/aaptcompiler/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ dependencies {
4444
testImplementation(libs.tests.junit)
4545
testImplementation(libs.tests.robolectric)
4646
testImplementation(libs.tests.google.truth)
47+
testImplementation(projects.shared)
4748
}

subprojects/aaptcompiler/src/test/java/com/itsaky/androidide/aaptcompiler/CompilerTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.android.aaptcompiler.compileResource
2626
import com.android.aaptcompiler.extractPathData
2727
import com.android.utils.StdLogger
2828
import com.android.utils.StdLogger.Level.VERBOSE
29+
import com.itsaky.androidide.utils.FileProvider
2930
import java.nio.file.Paths
3031
import kotlin.io.path.absolute
3132
import org.junit.Test
@@ -39,7 +40,7 @@ class CompilerTest {
3940
@Test
4041
fun `test simple compilatation`() {
4142
val input =
42-
Paths.get("../../app/src/main/res/layout/activity_editor.xml").absolute().normalize().toFile()
43+
FileProvider.projectRoot().resolve("app/src/main/res/layout/activity_editor.xml").toFile()
4344
val output = Paths.get("./build").absolute().normalize().toFile()
4445
println(input)
4546
println(output)

subprojects/projects/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ dependencies {
3434
implementation(libs.google.guava)
3535

3636
testImplementation(projects.subprojects.toolingApiTesting)
37+
testImplementation(projects.shared)
3738
testImplementation(libs.tests.junit)
3839
testImplementation(libs.tests.google.truth)
3940
testImplementation(libs.tests.robolectric)

subprojects/projects/src/test/java/com/itsaky/androidide/projects/api/ModuleProjectTest.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ import com.itsaky.androidide.projects.ProjectManager
2626
import com.itsaky.androidide.projects.builder.BuildService
2727
import com.itsaky.androidide.tooling.api.messages.InitializeProjectMessage
2828
import com.itsaky.androidide.tooling.testing.ToolingApiTestLauncher
29+
import com.itsaky.androidide.utils.FileProvider
2930
import com.itsaky.androidide.utils.SourceClassTrie.SourceNode
31+
import java.io.File
32+
import java.nio.file.Files
33+
import kotlin.io.path.pathString
34+
import kotlin.io.path.writeText
3035
import org.junit.Test
3136
import org.junit.runner.RunWith
3237
import org.robolectric.RobolectricTestRunner
3338
import org.robolectric.annotation.Config
34-
import java.io.File
35-
import java.nio.file.Files
36-
import java.nio.file.Paths
37-
import kotlin.io.path.writeText
3839

3940
/** @author Akash Yadav */
4041
@RunWith(RobolectricTestRunner::class)
@@ -44,7 +45,7 @@ class ModuleProjectTest {
4445
@Test
4546
fun test() {
4647
val (server, project) = ToolingApiTestLauncher().launchServer()
47-
server.initialize(InitializeProjectMessage(File("../../tests/test-project").absolutePath)).get()
48+
server.initialize(InitializeProjectMessage(FileProvider.testProjectRoot().pathString)).get()
4849

4950
Lookup.DEFAULT.register(BuildService.KEY_PROJECT_PROXY, project)
5051

@@ -56,8 +57,7 @@ class ModuleProjectTest {
5657

5758
val rootDir = root.projectDir
5859
assertThat(rootDir).isNotNull()
59-
assertThat(Files.isSameFile(rootDir.toPath(), File("../../tests/test-project").toPath()))
60-
.isTrue()
60+
assertThat(Files.isSameFile(rootDir.toPath(), FileProvider.testProjectRoot())).isTrue()
6161

6262
val app = root.findByPath(":app")
6363
assertThat(app).isNotNull()
@@ -183,7 +183,8 @@ class ModuleProjectTest {
183183
private fun verifyProjectManagerAPIs() {
184184

185185
val testCls =
186-
Paths.get("../../tests/test-project/app/src/main/java/com/itsaky/test/app/Test.java")
186+
FileProvider.testProjectRoot()
187+
.resolve("app/src/main/java/com/itsaky/test/app/Test.java")
187188
.toAbsolutePath()
188189
.normalize()
189190
val testCls_renamed = testCls.parent!!.resolve("TestRenamed.java")

subprojects/projects/src/test/java/com/itsaky/androidide/projects/classpath/ZipFileClasspathReaderTest.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package com.itsaky.androidide.projects.classpath
1919

2020
import com.google.common.truth.Truth.assertThat
21-
import java.io.File
21+
import com.itsaky.androidide.utils.FileProvider
2222
import org.junit.Test
2323
import org.junit.runner.RunWith
2424
import org.robolectric.RobolectricTestRunner
@@ -32,9 +32,12 @@ class ZipFileClasspathReaderTest {
3232
@Test
3333
fun testListClasses() {
3434
val classes =
35-
ZipFileClasspathReader().listClasses(
36-
listOf(File("../../tests/test-project/app/src/main/resources/android.jar"))
37-
)
35+
ZipFileClasspathReader()
36+
.listClasses(
37+
listOf(
38+
FileProvider.testProjectRoot().resolve("app/src/main/resources/android.jar").toFile()
39+
)
40+
)
3841

3942
val context = classes.firstOrNull { it.name == "android.content.Context" }
4043
assertThat(context).isNotNull()

subprojects/tooling-api-impl/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ dependencies {
6363
implementation(libs.tooling.gradleApi)
6464

6565
testImplementation(projects.subprojects.toolingApiTesting)
66+
testImplementation(projects.shared)
6667
testImplementation(libs.tests.junit)
6768
testImplementation(libs.tests.google.truth)
6869

0 commit comments

Comments
 (0)