11import { test , beforeEach , after , afterEach , describe } from 'node:test' ;
2- import { join } from 'node:path' ;
2+ import { dirname , join } from 'node:path' ;
33import assert from 'node:assert/strict' ;
44import { render , cleanup } from 'cli-testing-library' ;
55import {
@@ -10,6 +10,8 @@ import {
1010 removeTestDir ,
1111 retry ,
1212} from './test-utils.ts' ;
13+ import { fileURLToPath } from 'node:url' ;
14+ import { readFile , rm , writeFile } from 'node:fs/promises' ;
1315
1416// Final cleanup code after all tests
1517after ( async ( ) => {
@@ -42,10 +44,16 @@ test('iapp help command works', async () => {
4244} ) ;
4345
4446test ( 'iapp -v command works' , async ( ) => {
47+ const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
48+ const packageJson = JSON . parse (
49+ await readFile ( join ( __dirname , '../package.json' ) , 'utf-8' )
50+ ) ;
51+ const { version } = packageJson ;
52+
4553 const { findByText, debug, clear } = await render ( IAPP_COMMAND , [ '-v' ] , {
4654 cwd : testDir ,
4755 } ) ;
48- await findByText ( '1.3.3' ) ;
56+ await findByText ( version ) ;
4957 // debug();
5058 clear ( ) ;
5159} ) ;
@@ -123,8 +131,6 @@ describe('JavaScript iApp', () => {
123131 await checkDockerImageContent ( {
124132 dockerImageId,
125133 expectedFiles : [
126- 'Dockerfile' ,
127- 'README.md' ,
128134 'node_modules' ,
129135 'package-lock.json' ,
130136 'package.json' ,
@@ -194,8 +200,6 @@ describe('JavaScript iApp', () => {
194200 await checkDockerImageContent ( {
195201 dockerImageId,
196202 expectedFiles : [
197- 'Dockerfile' ,
198- 'README.md' ,
199203 'node_modules' ,
200204 'package-lock.json' ,
201205 'package.json' ,
@@ -254,7 +258,7 @@ describe('Python iApp', () => {
254258 // check built docker image content
255259 await checkDockerImageContent ( {
256260 dockerImageId,
257- expectedFiles : [ 'Dockerfile' , 'README.md' , ' requirements.txt', 'src' ] ,
261+ expectedFiles : [ 'requirements.txt' , 'src' ] ,
258262 } ) ;
259263 } ) ;
260264 } ) ;
@@ -318,8 +322,88 @@ describe('Python iApp', () => {
318322 // check built docker image content
319323 await checkDockerImageContent ( {
320324 dockerImageId,
321- expectedFiles : [ 'Dockerfile' , 'README.md' , 'requirements.txt' , 'src' ] ,
325+ expectedFiles : [ 'requirements.txt' , 'src' ] ,
326+ } ) ;
327+ } ) ;
328+ } ) ;
329+ } ) ;
330+ } ) ;
331+
332+ describe ( 'Custom app' , ( ) => {
333+ describe ( 'iapp test' , ( ) => {
334+ const projectName = 'test-iapp' ;
335+
336+ // Initialize a test iApp project before each test
337+ beforeEach ( async ( ) => {
338+ await initIappProject ( {
339+ testDir,
340+ projectName,
341+ template : 'JavaScript' ,
342+ projectType : 'Hello World' ,
343+ } ) ;
344+ } ) ;
345+
346+ test ( 'iapp test command works' , async ( ) => {
347+ // removed .dockerignore
348+ await rm ( join ( testDir , projectName , '.dockerignore' ) ) ;
349+ // changed Dockerfile to a custom one
350+ const dockerfileContent = await readFile (
351+ join ( testDir , projectName , 'Dockerfile' ) ,
352+ 'utf-8'
353+ ) ;
354+ const customDockerfileContent = dockerfileContent . replace (
355+ 'COPY src/ ./src/' ,
356+ 'COPY . ./'
357+ ) ;
358+ await writeFile (
359+ join ( testDir , projectName , 'Dockerfile' ) ,
360+ customDockerfileContent ,
361+ 'utf-8'
362+ ) ;
363+
364+ const { findByText, debug, clear, userEvent, getStdallStr } =
365+ await render ( IAPP_COMMAND , [ 'test' ] , {
366+ cwd : join ( testDir , projectName ) ,
322367 } ) ;
368+ // wait for docker build and test run
369+ await retry ( ( ) => findByText ( 'Would you like to see the app logs?' ) , {
370+ retries : 8 ,
371+ delay : 3000 ,
372+ } ) ;
373+ // extract docker image id from stdout
374+ const std = getStdallStr ( ) ;
375+ const dockerImageIdMatch = std . match (
376+ / A p p d o c k e r i m a g e b u i l t \( s h a 2 5 6 : [ a - f 0 - 9 ] { 64 } \) /
377+ ) ;
378+ assert . ok ( dockerImageIdMatch , 'Docker image ID not found in output' ) ;
379+ const dockerImageId = dockerImageIdMatch ! [ 0 ] . split ( '(' ) [ 1 ] . slice ( 0 , - 1 ) ;
380+
381+ // debug();
382+ clear ( ) ;
383+ userEvent . keyboard ( 'n' ) ;
384+ await findByText ( 'Would you like to see the result?' ) ;
385+ // debug();
386+ clear ( ) ;
387+ userEvent . keyboard ( 'n' ) ;
388+ await findByText ( 'When ready run iapp deploy' ) ;
389+ // debug();
390+ clear ( ) ;
391+
392+ // check built docker image content
393+ await checkDockerImageContent ( {
394+ dockerImageId,
395+ expectedFiles : [
396+ 'Dockerfile' ,
397+ 'README.md' ,
398+ 'cache' ,
399+ 'input' ,
400+ 'mock' ,
401+ 'node_modules' ,
402+ 'output' ,
403+ 'package-lock.json' ,
404+ 'package.json' ,
405+ 'src' ,
406+ ] ,
323407 } ) ;
324408 } ) ;
325409 } ) ;
0 commit comments