Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying raw file name #1085

Open
ansman opened this issue Sep 13, 2023 · 7 comments
Open

Allow specifying raw file name #1085

ansman opened this issue Sep 13, 2023 · 7 comments
Labels
enhancement New feature or request

Comments

@ansman
Copy link
Contributor

ansman commented Sep 13, 2023

I run my tests based on java resource files using a parameterized test. So all my tests run from a single method.

This is problematic because Paparazzi will prepend the test package name, class name and method name to the output file. To make matters worse, the output file name is often too long since I use the FQN of the composable being screenshotted.

Specifying a name doesn't work as it only appends it to the default name.

There should be a way to specify the full file name without having the class name as a prefix.

Here is some code to illustrate the problem:

@RunWith(Parameterized::class)
class PaparazziScreenshotTests(private val test: TestCase) {
    @get:Rule
    val paparazzi = Paparazzi(...)

    @Test
    fun runTest() {
        paparazzi.snapshot {
            ...
        }
    }

    companion object {
        @JvmStatic
        @Parameterized.Parameters(name = "{0}")
        fun testCases() = listResources().map(::TestCase)
    }

    class TestCase(
        val className: String,
        val methodName: String,
    ) {
        override fun toString(): String = buildString {
            append(className)
            append('.')
            append(methodName)
        }
    }
}

This will generate files that look like this:

com.example_PaparazziScreenshotTests_runTest[com.example.SomeComposableKt.SomeComposable].png
@ansman ansman added the enhancement New feature or request label Sep 13, 2023
@ansman
Copy link
Contributor Author

ansman commented Sep 13, 2023

I'm happy to provide a PR that either allows disabling the prepending or a way to specify the full filename.

@borsini
Copy link

borsini commented Oct 6, 2023

Such an addition would be awesome. @ansman do you plan on opening a PR?

@ansman
Copy link
Contributor Author

ansman commented Oct 6, 2023

No plans until the maintainers sign off on the idea

@epool
Copy link

epool commented Dec 8, 2023

I did something similar using the Showkase library and following the Paparazzi sample as reference

companion object {
    @JvmStatic
    @Parameterized.Parameters(name = "{0}")
    fun parameters() = Showkase.getMetadata()
        .componentList
        .map(::ComponentTestPreview)
        .toList()
        .toTypedArray()
}

class ComponentTestPreview(
    private val showkaseBrowserComponent: ShowkaseBrowserComponent
) {
    private val showkaseComponentNameParts = showkaseBrowserComponent.componentKey.split("_")

    private val packageName: String
        get() = showkaseComponentNameParts[0]

    private val componentName: String
        get() = showkaseComponentNameParts[1]

    private val parameterizedIndex: String
        get() = showkaseComponentNameParts.getOrNull(7)?.let { "_$it" }.orEmpty()

    @Composable
    fun Content() = showkaseBrowserComponent.component()

    override fun toString(): String =
        "$packageName.$componentName$parameterizedIndex"
}

@skylerreimer
Copy link

skylerreimer commented May 21, 2024

Hello from Amazon!

Bumping this issue as well. We have a compose component library that uses paparazzi to do our screenshot testing. It would be nice if we could pass a parameter to our tests to set what file location the screenshots are placed in. We have tests for RTL, Dark/Light mode, font scaling etc for each of our components on top of all of the different parameters our components support. This gives us 1000+ screenshots in our repo all dumped into a single folder with difficult names. Paparazzi gets harder to use the more screenshots you have in your repo. I suppose maybe we could combine our screenshots into single images for all variants, but that makes our tests harder to maintain in code.

My ask is to be able to organize our screenshots into some sort of folder structure AND rename the files. This would let me continue to have one test for one screenshot. Our CI is rough to use sometimes so it's nice to know that ComponentA in dark mode with parameter X has failed so I can run that test locally to fix the issue.

I'm happy to do this contribution myself, but wanting to get sign off before I spend the time. Amazon's open source contribution policy is annoying to work around and I don't want to waste my time if my PR isn't going to be accepted.

LMK what you think!

@VirtualParticle
Copy link

+1 for allowing a different file structure to organize screenshots. By replacing the delimiters in a few places it's possible to make it generate an output like one of these two options, whichever seems more convenient.

image image

This seems to be the same issue as #549

@leo-beliakov
Copy link

@jrodbx Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants