Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Fix NPE when listing files in screenshot folder #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rafakob
Copy link

@rafakob rafakob commented Feb 9, 2018

listFiles() may return null which leads to crash:

Exception in thread "main" java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.collections.ArraysKt___ArraysKt.toList, parameter $receiver
        at kotlin.collections.ArraysKt___ArraysKt.toList(_Arrays.kt)
        at com.gojuno.composer.TestRunKt$pullTestFiles$3.call(TestRun.kt:177)
        at com.gojuno.composer.TestRunKt$pullTestFiles$3.call(TestRun.kt)

@@ -173,7 +173,7 @@ private fun pullTestFiles(adbDevice: AdbDevice, test: InstrumentationTest, outpu
PulledFiles(
files = emptyList(), // TODO: Pull test files.
screenshots = screenshotsFolderOnHostMachine.let {
when (it.exists()) {
when (it.exists() && it.listFiles() != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listFiles() can return null only if it is not a directory. Calling exists() beforehand is partially redundant IMHO.
There is also a race condition possible when directory is removed in the time between two listFiles() calls.

What about replacing when block with it.listFiles()?.toList() ?: emptyList() ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about when (it.exists() && it.isDirectory())?

Race condition is still possible with it.listFiles()?.toList() ?: emptyList() since we're going to get problems later during adb pull if list of files is invalid

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isDirectory() returns false if there is no directory under given path including the case when there is nothing there.

we're going to get problems later during adb pull if list of files is invalid

It seems that screenshots have been already pulled when we are listing those files:
https://github.com/rafakob/composer/blob/f70e773c73adc6e7f98ffaf85f0000d5f8675ee4/composer/src/main/kotlin/com/gojuno/composer/TestRun.kt#L164

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah right, my bad

returns false if there is no directory under given path including the case when there is nothing there.

Isn't that what we want?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but listFiles() != null also checks for existence.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well for sake of removing redundancy from if statement I'm 👍 with using listFiles() != null

@plastiv
Copy link
Contributor

plastiv commented Mar 18, 2019

How to reproduce this NPE?

it.listFiles() can return null only when it is file instead of folder. Who is creating it as a file?

Composer code doesn't seem to be touching it, creating parent testClassName folder only. So it looks like file was downloaded by adb pull. Which could happen only when not complying to spoon screenshot contract

storage/sdcard/app_spoon-screenshots/package.testClassName/testMethodName/screenshotName.png`

In this case, I think proposed solution to silently ignoring NPE is incorrect. Because storage/sdcard/app_spoon-screenshots/package.testClassName/testMethodName screenshot is not getting merged into html report. Instead, I would prefer to have an assertion message to avoid debugging why.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants