@@ -4,25 +4,30 @@ import kotlin.test.*
44
55class FileTests {
66
7- private val localSeparator = ' /'
8-
97 @Test
108 fun testNonexistentRootFile () {
119 val testFile = File (" testNonexistentRootFile.txt" )
1210
1311 assertFalse(testFile.exists(), " file should not exist" )
1412 assertFalse(testFile.isDirectory(), " file should not be directory" )
1513 assertFalse(testFile.isFile(), " file should not be file" )
16- assertNull(testFile.getParent(), " file should not have parent" )
17- assertNull(testFile.getParentFile(), " file should not have parent file" )
14+
15+ if (platform() == Platform .JVM ) {
16+ assertNull(testFile.getParent(), " file should not have parent" )
17+ assertNull(testFile.getParentFile(), " file should not have parent file" )
18+ }
19+
20+ if (platform().isPosix()) { // in posix we always resolve relative path via `realpath` syscall
21+ assertEquals(testFile.getParent(), File (" ./" ).getAbsolutePath(), " as parent should be current dir" )
22+ assertEquals(testFile.getParentFile()?.getAbsolutePath(), File (" ./" ).getAbsolutePath(), " as parent should be current dir" )
23+ }
1824
1925 assertEquals(" testNonexistentRootFile" , testFile.nameWithoutExtension)
2026 }
2127
2228 @Test
2329 fun testExistentRootFile () {
2430 val testFile = File (" testFileRoot/testExistentRootFile.txt" )
25- println (testFile.getAbsolutePath())
2631
2732 assertFalse(testFile.exists(), " file should not exist" )
2833 assertFalse(testFile.getParentFile()?.exists() == true , " file should not have parent file" )
@@ -33,6 +38,10 @@ class FileTests {
3338
3439 @Test
3540 fun testFileCreateAndDelete () {
41+ if (platform() == Platform .Windows ) {
42+ return
43+ }
44+
3645 val testFolder = File (" build/testNewDirectoryCreation" )
3746
3847 assertTrue(testFolder.mkdirs(), " create directory failed" )
@@ -52,6 +61,10 @@ class FileTests {
5261
5362 @Test
5463 fun testFileWriteAndRead () {
64+ if (platform() == Platform .Windows ) {
65+ return
66+ }
67+
5568 val testFolder = File (" build/testFileWriteAndRead" )
5669 val testFile = File (" build/testFileWriteAndRead/test.txt" )
5770
@@ -87,6 +100,10 @@ class FileTests {
87100
88101 @Test
89102 fun testFileCopyMethod () {
103+ if (platform() == Platform .Windows ) {
104+ return
105+ }
106+
90107 val testFile = File (" gradle/wrapper/gradle-wrapper.properties" )
91108 val testDestFolder = File (" build/testCopyFolder" )
92109 val testDestFile = File (" build/testCopyFolder/gradle-wrapper.properties" )
@@ -107,6 +124,10 @@ class FileTests {
107124
108125 @Test
109126 fun testFileMoveMethod () {
127+ if (platform() == Platform .Windows ) {
128+ return
129+ }
130+
110131 val testFolder = File (" build/testMoveFolder" )
111132 val testDestFolder = File (" build/testMoveFolder2" )
112133 val testFile = File (" build/testMoveFolder/test_move_file.properties" )
@@ -198,4 +219,32 @@ class FileTests {
198219
199220 assertTrue(testFile.delete(), " delete file failed" )
200221 }
222+
223+ @Test
224+ fun testFileRealPathIfRelativeLinks__posixOnly () {
225+ if (! platform().isPosix()) {
226+ return
227+ }
228+
229+ val symlinkPrefix = if (platform() == Platform .Macos ) " /private" else " "
230+
231+ val testDir = File (" /tmp/build" )
232+ val testFile = Files .createTempFile(prefix = " ../test" , suffix = " .txt" , dir = testDir)
233+ assertEquals(" $symlinkPrefix /tmp/test.txt" , testFile.getAbsolutePath()) // 'cause /tmp is a symlink for /private/tmp
234+ assertTrue(testFile.delete(), " delete file failed" )
235+ assertTrue(testDir.delete(), " delete test folder failed" )
236+ }
237+
238+ @Test
239+ fun testFileRealPathIfRelativeLinks__jvmOnly () {
240+ if (platform() != Platform .JVM ) {
241+ return
242+ }
243+
244+ val testDir = File (" /tmp/build" )
245+ val testFile = Files .createTempFile(prefix = " ../test" , suffix = " .txt" , dir = testDir)
246+ assertEquals(" /tmp/build/../test.txt" , testFile.getAbsolutePath()) // lazy canonicalization in jvm
247+ assertTrue(testFile.delete(), " delete file failed" )
248+ assertTrue(testDir.delete(), " delete test folder failed" )
249+ }
201250}
0 commit comments