-
Notifications
You must be signed in to change notification settings - Fork 277
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
Jimfs support for JUnit 5 TempDirFactory
#258
Comments
Interesting, thanks. Cool that jimfs makes the JUnit docs! We're not JUnit 5 users (yet? unclear :)), so it probably won't be a priority, but that's not to rule it out entirely. (If nothing else, it makes it harder for us to judge design questions like "Should it be able to use a |
I see, at least you're now aware of it 🙂
Not sure I fully understood what you mean. I personally consider relying on an in-memory file system in tests a common use case and that was one of my motivations for the changes I proposed to the JUnit 5 team. Just to point to a real-life example of a |
Sorry. I'm not sure if I understand what I mean, either :) My thinking was roughly that there might be a way to write a single annotation that could tell JUnit to use an arbitrary given |
To demonstrate real-life demand I'll AOL this thread #metoo :) It'd be great to have such a provider, it'd make Jimfs even easier to use. |
Thanks for the JUnit5 example (GitHub link) @scordio 👍 I came along with this version, which also adds fields to set the System (Win, Osx, Linux) and the root dir name: // Kotlin
@Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER)
@Retention(AnnotationRetention.RUNTIME)
@TempDir(factory = JimfsTempDirFactory::class)
annotation class JimfsTempDir(
val system: FileSystem = FileSystem.Unix,
val folder: String = "junit"
) {
enum class FileSystem { Unix, OsX, Windows }
}
class JimfsTempDirFactory : TempDirFactory {
private val fileSystemUnix: Lazy<FileSystem> = lazy { Jimfs.newFileSystem(Configuration.unix()) }
private val fileSystemOsX: Lazy<FileSystem> = lazy { Jimfs.newFileSystem(Configuration.osX()) }
private val fileSystemWindows: Lazy<FileSystem> = lazy { Jimfs.newFileSystem(Configuration.windows()) }
override fun createTempDirectory(elementContext: AnnotatedElementContext, extensionContext: ExtensionContext): Path {
val tempDirAnnotation = elementContext.findAnnotation<JimfsTempDir>(JimfsTempDir::class.java).orElseThrow()
val fileSystemType = tempDirAnnotation.system
val folder = tempDirAnnotation.folder
return Files.createTempDirectory(getFileSystem(fileSystemType).getPath("/"), folder);
}
fun getFileSystem(system: JimfsTempDir.FileSystem) = when (system) {
JimfsTempDir.FileSystem.Unix -> fileSystemUnix
JimfsTempDir.FileSystem.OsX -> fileSystemOsX
JimfsTempDir.FileSystem.Windows -> fileSystemWindows
}.value
override fun close() {
listOf(fileSystemUnix, fileSystemOsX, fileSystemWindows)
.filter { it.isInitialized() }.forEach { it.value.close() }
}
} Feedback for the
|
As @cpovirk mentioned, first-party support might be provided only once Google moves to JUnit 5. As I already had a first draft of an extension based on Jimfs, I decided to push it forward. I'll continue polishing it (README and Javadoc are currently missing 🙂 ) and likely release the first version at the beginning of the week. A simple usage example: class JimfsTempDirTests {
@JimfsTempDir(WINDOWS) // parameter is optional, can also be FOR_CURRENT_PLATFORM, OS_X, UNIX
Path tempDir;
@Test
void test() {
assertThat(tempDir.getFileSystem().provider().getScheme()).isEqualTo("jimfs");
assertThat(tempDir.getFileSystem().getSeparator()).isEqualTo("\\");
}
} In case you'd like a first look, here it is: https://github.com/scordio/jimfs-junit-jupiter Feedback appreciated! |
@TobseF about your feedback:
By default, JUnit creates a new temporary directory for each
With this, there should be no need to create multiple file systems in the same factory instance. Feel free to check how I approached it: https://github.com/scordio/jimfs-junit-jupiter/blob/6511f81cbb48267dd1a4dec3e93c575a280fec43/src/main/java/io/github/scordio/jimfs/junit/jupiter/JimfsTempDirFactory.java#L37-L50 About something else entirely, I noticed in your snippet that you declared a |
And here is the first release: https://github.com/scordio/jimfs-junit-jupiter/releases/tag/v0.1.0 Any feedback is welcome! |
Starting from version 5.10, JUnit 5 offers a
TempDirFactory
SPI for customizing how temporary directories are created via the@TempDir
annotation.The SPI allows libraries like Jimfs to provide their own implementation, which can be used:
@TempDir
annotation via thefactory
attribute, e.g.:You might notice that the JUnit documentation already refers to Jimfs to demonstrate certain use cases.
Would you consider first-party support for such a feature, providing your own factory implementation and maybe even meta-annotation(s)?
Disclaimer: I authored most of the
TempDirFactory
changes so happy to hear your feedback and report back improvements, if you see any 🙂The text was updated successfully, but these errors were encountered: