@@ -590,4 +590,133 @@ void deleteDirectory(File dir) throws IOException {
590590 .map (Path ::toFile ).forEach (File ::delete );
591591 }
592592
593+ @ Test
594+ public void verifyPackageLockAndClean_lockfileVersion3_fileNotRemoved ()
595+ throws IOException {
596+ File packageLockFile = new File (npmFolder , "package-lock.json" );
597+ String packageLockContent = """
598+ {
599+ "name": "test-project",
600+ "version": "1.0.0",
601+ "lockfileVersion": 3,
602+ "requires": true,
603+ "packages": {}
604+ }
605+ """ ;
606+ FileUtils .write (packageLockFile , packageLockContent ,
607+ StandardCharsets .UTF_8 );
608+
609+ task .verifyPackageLockAndClean ();
610+
611+ Assert .assertTrue (
612+ "package-lock.json with version 3 should not be removed" ,
613+ packageLockFile .exists ());
614+ }
615+
616+ @ Test
617+ public void verifyPackageLockAndClean_lockfileVersion2_fileRemoved ()
618+ throws IOException {
619+ File packageLockFile = new File (npmFolder , "package-lock.json" );
620+ String packageLockContent = """
621+ {
622+ "name": "test-project",
623+ "version": "1.0.0",
624+ "lockfileVersion": 2,
625+ "requires": true,
626+ "packages": {}
627+ }
628+ """ ;
629+ FileUtils .write (packageLockFile , packageLockContent ,
630+ StandardCharsets .UTF_8 );
631+
632+ task .verifyPackageLockAndClean ();
633+
634+ Assert .assertFalse ("package-lock.json with version 2 should be removed" ,
635+ packageLockFile .exists ());
636+ }
637+
638+ @ Test
639+ public void verifyPackageLockAndClean_lockfileVersion1_fileRemoved ()
640+ throws IOException {
641+ File packageLockFile = new File (npmFolder , "package-lock.json" );
642+ String packageLockContent = """
643+ {
644+ "name": "test-project",
645+ "version": "1.0.0",
646+ "lockfileVersion": 1,
647+ "requires": true,
648+ "dependencies": {}
649+ }
650+ """ ;
651+ FileUtils .write (packageLockFile , packageLockContent ,
652+ StandardCharsets .UTF_8 );
653+
654+ task .verifyPackageLockAndClean ();
655+
656+ Assert .assertFalse ("package-lock.json with version 1 should be removed" ,
657+ packageLockFile .exists ());
658+ }
659+
660+ @ Test
661+ public void verifyPackageLockAndClean_withSpaces_correctlyParsed ()
662+ throws IOException {
663+ File packageLockFile = new File (npmFolder , "package-lock.json" );
664+ String packageLockContent = """
665+ {
666+ "name": "test-project",
667+ "version": "1.0.0",
668+ "lockfileVersion" : 3,
669+ "requires": true,
670+ "packages": {}
671+ }
672+ """ ;
673+ FileUtils .write (packageLockFile , packageLockContent ,
674+ StandardCharsets .UTF_8 );
675+
676+ task .verifyPackageLockAndClean ();
677+
678+ Assert .assertTrue (
679+ "package-lock.json with version 3 (with spaces) should not be removed" ,
680+ packageLockFile .exists ());
681+ }
682+
683+ @ Test
684+ public void verifyPackageLockAndClean_pnpmEnabled_fileNotChecked ()
685+ throws IOException {
686+ options .withEnablePnpm (true );
687+ task = createTask (new ArrayList <>());
688+
689+ File packageLockFile = new File (npmFolder , "package-lock.json" );
690+ String packageLockContent = """
691+ {
692+ "name": "test-project",
693+ "version": "1.0.0",
694+ "lockfileVersion": 2,
695+ "requires": true,
696+ "packages": {}
697+ }
698+ """ ;
699+ FileUtils .write (packageLockFile , packageLockContent ,
700+ StandardCharsets .UTF_8 );
701+
702+ task .verifyPackageLockAndClean ();
703+
704+ Assert .assertTrue (
705+ "package-lock.json should not be checked when pnpm is enabled" ,
706+ packageLockFile .exists ());
707+ }
708+
709+ @ Test
710+ public void verifyPackageLockAndClean_noLockfile_doesNotThrow () {
711+ File packageLockFile = new File (npmFolder , "package-lock.json" );
712+ Assert .assertFalse ("package-lock.json should not exist" ,
713+ packageLockFile .exists ());
714+
715+ // Should not throw any exception
716+ task .verifyPackageLockAndClean ();
717+
718+ Assert .assertFalse ("package-lock.json should still not exist" ,
719+ packageLockFile .exists ());
720+ }
721+
593722}
0 commit comments