11import fs from 'node:fs'
22import path from 'node:path'
33import { afterAll , beforeAll , describe , expect , it } from 'vitest'
4- import { createRollupInputConfig , readPropertiesFile } from '../src/utils'
4+ import { createRollupInputConfig , javaVersion , readPropertiesFile } from '../src/utils'
55
66const files = [
77 'src/main.ts' ,
@@ -10,6 +10,7 @@ const files = [
1010 'src/nested/main.ts' ,
1111 'src/nested/other.ts' ,
1212]
13+ const TEMP_FIXTURES_FOLDER = '__fixtures__'
1314
1415function getAbsolutePath ( filepath : string ) {
1516 return path . normalize ( path . resolve ( process . cwd ( ) , filepath ) )
@@ -18,30 +19,30 @@ function getAbsolutePath(filepath: string) {
1819describe ( 'vite-plugin-java - utils' , ( ) => {
1920 beforeAll ( ( ) => {
2021 for ( const file of files ) {
21- const filePath = path . join ( 'fixtures' , file )
22+ const filePath = path . join ( TEMP_FIXTURES_FOLDER , file )
2223 fs . mkdirSync ( path . dirname ( filePath ) , { recursive : true } )
2324 fs . writeFileSync ( filePath , '/** I love Java! **/' )
2425 }
2526 } )
2627
2728 afterAll ( ( ) => {
28- fs . rmSync ( 'fixtures' , { recursive : true , force : true } )
29+ fs . rmSync ( TEMP_FIXTURES_FOLDER , { recursive : true , force : true } )
2930 } )
3031
3132 it ( 'should find all the entry files' , ( ) => {
32- const inputs = createRollupInputConfig ( 'fixtures /src/**/*.ts' , 'fixtures /src' )
33+ const inputs = createRollupInputConfig ( ` ${ TEMP_FIXTURES_FOLDER } /src/**/*.ts` , ` ${ TEMP_FIXTURES_FOLDER } /src` )
3334
3435 expect ( inputs ) . toEqual ( {
35- main : getAbsolutePath ( 'fixtures /src/main.ts' ) ,
36- other : getAbsolutePath ( 'fixtures /src/other.ts' ) ,
37- another : getAbsolutePath ( 'fixtures /src/another.ts' ) ,
38- [ path . normalize ( 'nested/main' ) ] : getAbsolutePath ( 'fixtures /src/nested/main.ts' ) ,
39- [ path . normalize ( 'nested/other' ) ] : getAbsolutePath ( 'fixtures /src/nested/other.ts' ) ,
36+ main : getAbsolutePath ( ` ${ TEMP_FIXTURES_FOLDER } /src/main.ts` ) ,
37+ other : getAbsolutePath ( ` ${ TEMP_FIXTURES_FOLDER } /src/other.ts` ) ,
38+ another : getAbsolutePath ( ` ${ TEMP_FIXTURES_FOLDER } /src/another.ts` ) ,
39+ [ path . normalize ( 'nested/main' ) ] : getAbsolutePath ( ` ${ TEMP_FIXTURES_FOLDER } /src/nested/main.ts` ) ,
40+ [ path . normalize ( 'nested/other' ) ] : getAbsolutePath ( ` ${ TEMP_FIXTURES_FOLDER } /src/nested/other.ts` ) ,
4041 } )
4142 } )
4243
4344 it ( 'should find all the entry files with cwd setup' , ( ) => {
44- const inputs = createRollupInputConfig ( 'src/**/main.ts' , 'src' , { cwd : 'fixtures' } )
45+ const inputs = createRollupInputConfig ( 'src/**/main.ts' , 'src' , { cwd : TEMP_FIXTURES_FOLDER } )
4546
4647 expect ( inputs ) . toEqual ( {
4748 main : getAbsolutePath ( 'src/main.ts' ) ,
@@ -61,9 +62,42 @@ describe('vite-plugin-java - utils', () => {
6162 for ( const [ key , value ] of expectedProperties . entries ( ) ) {
6263 content += `${ key } =${ value } \n`
6364 }
64- fs . writeFileSync ( 'fixtures /application.properties' , content )
65+ fs . writeFileSync ( ` ${ TEMP_FIXTURES_FOLDER } /application.properties` , content )
6566
6667 const propertiesMap = readPropertiesFile ( )
6768 expect ( propertiesMap ) . toEqual ( expectedProperties )
6869 } )
70+
71+ it ( 'should read the Java version from a Maven project' , ( ) => {
72+ // set up as a Maven project
73+ fs . copyFileSync ( path . resolve ( __dirname , 'fixtures/pom.xml' ) , `${ TEMP_FIXTURES_FOLDER } /pom.xml` )
74+
75+ const expectedVersion = '11'
76+ const version = javaVersion ( TEMP_FIXTURES_FOLDER )
77+ expect ( version ) . toBe ( expectedVersion )
78+
79+ fs . rmSync ( `${ TEMP_FIXTURES_FOLDER } /pom.xml` )
80+ } )
81+
82+ it ( 'should read the Java version from a Gradle project' , ( ) => {
83+ // set up as a Gradle project
84+ fs . copyFileSync ( path . resolve ( __dirname , 'fixtures/build.gradle' ) , `${ TEMP_FIXTURES_FOLDER } /build.gradle` )
85+
86+ const expectedVersion = '1.8'
87+ const version = javaVersion ( TEMP_FIXTURES_FOLDER )
88+ expect ( version ) . toBe ( expectedVersion )
89+
90+ fs . rmSync ( `${ TEMP_FIXTURES_FOLDER } /build.gradle` )
91+ } )
92+
93+ it ( 'should read the Java version from a Kotlin DSL project' , ( ) => {
94+ // set up as a Kotlin DSL project
95+ fs . copyFileSync ( path . resolve ( __dirname , 'fixtures/build.gradle.kts' ) , `${ TEMP_FIXTURES_FOLDER } /build.gradle.kts` )
96+
97+ const expectedVersion = '21'
98+ const version = javaVersion ( TEMP_FIXTURES_FOLDER )
99+ expect ( version ) . toBe ( expectedVersion )
100+
101+ fs . rmSync ( `${ TEMP_FIXTURES_FOLDER } /build.gradle.kts` )
102+ } )
69103} )
0 commit comments