5

I've created a function that copies a file from assets to the internal path in my instrumented tests.

fun copyFileFromAssetsToInternal(context: Context, fis: FileInputStream) {
        File(context.filesDir, file).outputStream().use { fos ->
            var read: Int
            val buffer = ByteArray(1024)
            do {
                read = fis.read(buffer)
                if (read == -1) break
                fos.write(buffer, 0, read)
            } while (read > 0)
        }
}

my application's package is gr.example.myapp

when I use InstrumentationRegistry.getInstrumentation().targetContext the above works normally but it writes to /data/user/0/gr.example.myapp/files/

but when I use InstrumentationRegistry.getInstrumentation().context the above tries to write to /data/user/0/gr.example.myapp.test/files/ (which I have checked and it indeed does exist) and fails with with the following exception:

java.io.FileNotFoundException: /data/user/0/gr.example.myapp.test/files/test.zip (Permission denied)

I have added

@get:Rule
    val rule = GrantPermissionRule.grant(permission.WRITE_EXTERNAL_STORAGE, permission.READ_EXTERNAL_STORAGE)

to the top of my test file and I still get the error

is there something I can do? cause I don't really want to contaminate my actual application's internal data with those of the tests

Cruces
  • 3,029
  • 1
  • 26
  • 55

0 Answers0