@@ -8,16 +8,21 @@ import * as Fiber from "effect/Fiber";
88import * as Path from "effect/Path" ;
99import * as TestClock from "effect/testing/TestClock" ;
1010import { HttpClient , HttpClientResponse } from "effect/http" ;
11+ import * as ChildProcess from "effect/process/ChildProcess" ;
1112import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
1213
14+ import * as HostProcess from "@t3tools/shared/HostProcess" ;
15+
1316import * as ProcessRunner from "../processRunner.ts" ;
1417import {
1518 ensurePinnedRuntimeInstalled ,
1619 pinnedRuntimeCommand ,
1720 pinnedRuntimePaths ,
1821 PinnedRuntimeInstallError ,
1922 type PinnedRuntimeProgress ,
23+ prunePinnedRuntimes ,
2024} from "./pinnedRuntime.ts" ;
25+ import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts" ;
2126
2227const version = "1.2.3" ;
2328const archiveName = `t3-${ version } -linux-x64.tar.gz` ;
@@ -553,3 +558,205 @@ it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
553558 } ) ,
554559 ) ;
555560} ) ;
561+
562+ it . layer ( NodeServices . layer ) ( "prunePinnedRuntimes" , ( it ) => {
563+ const lsofRunner = ( stdout : string ) =>
564+ ProcessRunner . ProcessRunner . of ( {
565+ run : ( ) =>
566+ Effect . succeed ( {
567+ stdout,
568+ stderr : "" ,
569+ code : ChildProcessSpawner . ExitCode ( 0 ) ,
570+ timedOut : false ,
571+ stdoutTruncated : false ,
572+ stderrTruncated : false ,
573+ stdoutInvalidUtf8 : false ,
574+ stderrInvalidUtf8 : false ,
575+ } ) ,
576+ } ) ;
577+
578+ const setup = Effect . fn ( function * ( versions : ReadonlyArray < string > ) {
579+ const fs = yield * FileSystem . FileSystem ;
580+ const path = yield * Path . Path ;
581+ const baseDir = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-pinned-prune-" } ) ;
582+ const versionsDir = path . join ( baseDir , "runtime" , "versions" ) ;
583+ const now = Duration . toMillis ( Duration . days ( 1 ) ) ;
584+ yield * TestClock . setTime ( now ) ;
585+ const install = Effect . fn ( function * (
586+ version : string ,
587+ completed : "long ago" | "just now" | "never" = "long ago" ,
588+ ) {
589+ const directory = path . join ( versionsDir , version ) ;
590+ yield * fs . makeDirectory ( directory , { recursive : true } ) ;
591+ yield * fs . writeFileString ( path . join ( directory , "t3" ) , "" ) ;
592+ if ( completed === "never" ) return ;
593+ const sentinel = path . join ( directory , ".install-complete" ) ;
594+ yield * fs . writeFileString ( sentinel , `${ version } \n` ) ;
595+ const at = completed === "just now" ? now : 0 ;
596+ yield * fs . utimes ( sentinel , at , at ) ;
597+ } ) ;
598+ for ( const version of versions ) yield * install ( version ) ;
599+ return { baseDir, versionsDir, install } ;
600+ } ) ;
601+
602+ it . effect ( "removes only completed runtimes nothing references" , ( ) =>
603+ Effect . gen ( function * ( ) {
604+ const fs = yield * FileSystem . FileSystem ;
605+ const path = yield * Path . Path ;
606+ const { baseDir, versionsDir, install } = yield * setup ( [
607+ "1.0.0" ,
608+ "1.1.0" ,
609+ "1.2.0" ,
610+ "1.2.5" ,
611+ "1.3.0" ,
612+ "1.3.5" ,
613+ "1.4.0" ,
614+ "1.5.0" ,
615+ "2.0.0" ,
616+ "2.1.0" ,
617+ ] ) ;
618+ // A downgrade another process installed but has not pointed anything at yet.
619+ yield * install ( "0.5.0" , "just now" ) ;
620+ yield * install ( "1.0.1" , "never" ) ;
621+ yield * install ( ".staging-abc" , "never" ) ;
622+ yield * install ( ".pruned-0.8.0" ) ;
623+ yield * install ( "other" , "never" ) ;
624+ yield * fs . symlink ( path . join ( versionsDir , "other" ) , path . join ( versionsDir , "0.9.0" ) ) ;
625+ const binDir = path . join ( baseDir , "bin" ) ;
626+ const customBinDir = path . join ( baseDir , "custom-bin" ) ;
627+ yield * fs . makeDirectory ( binDir ) ;
628+ yield * fs . makeDirectory ( customBinDir ) ;
629+ // Chained: the launcher reaches its runtime through a second link.
630+ yield * fs . symlink ( path . join ( versionsDir , "1.2.0" , "t3" ) , path . join ( baseDir , "alias-t3" ) ) ;
631+ yield * fs . symlink ( path . join ( baseDir , "alias-t3" ) , path . join ( binDir , "t3" ) ) ;
632+ yield * fs . symlink ( path . join ( versionsDir , "1.2.5" , "t3" ) , path . join ( customBinDir , "t3" ) ) ;
633+ yield * fs . writeFileString (
634+ path . join ( baseDir , "runtime" , "launchers" ) ,
635+ `${ path . join ( customBinDir , "t3" ) } \n` ,
636+ ) ;
637+ yield * fs . writeFileString (
638+ path . join ( baseDir , "runtime" , "service-state.json" ) ,
639+ JSON . stringify ( {
640+ protocol : SERVICE_LAUNCHER_PROTOCOL ,
641+ activeVersion : "1.4.0" ,
642+ update : { id : "u" , fromVersion : "1.4.0" , targetVersion : "1.5.0" , status : "rolled-back" } ,
643+ } ) ,
644+ ) ;
645+
646+ const removed = yield * prunePinnedRuntimes ( {
647+ baseDir,
648+ keep : [ "2.0.0" ] ,
649+ references : [ `ExecStart=${ path . join ( versionsDir , "1.3.5" , "t3" ) } __service-launcher` ] ,
650+ } ) . pipe (
651+ Effect . provideService (
652+ ProcessRunner . ProcessRunner ,
653+ lsofRunner ( `p42\nftxt\nn${ path . join ( versionsDir , "1.3.0" , "t3" ) } \n` ) ,
654+ ) ,
655+ Effect . provideService ( HostProcess . Platform , "darwin" ) ,
656+ Effect . provideService ( HostProcess . Environment , { T3CODE_INSTALL_BIN_DIR : binDir } ) ,
657+ ) ;
658+
659+ // 1.2.0 is the default launcher, 1.2.5 a recorded one, 1.3.0 running, 1.3.5 in the unit,
660+ // 1.4.0 active, 1.5.0 the rolled-back target, 2.0.0 the caller's, 0.5.0 just installed.
661+ // Being newer than the caller's version does not keep 2.1.0.
662+ assert . deepEqual ( removed . toSorted ( ) , [ "0.8.0" , "1.0.0" , "1.1.0" , "2.1.0" ] ) ;
663+ // The launcher found on PATH is remembered for a service that cannot see it.
664+ assert . equal (
665+ yield * fs . readFileString ( path . join ( baseDir , "runtime" , "launchers" ) ) ,
666+ `${ path . join ( customBinDir , "t3" ) } \n${ path . join ( binDir , "t3" ) } \n` ,
667+ ) ;
668+ assert . deepEqual ( ( yield * fs . readDirectory ( versionsDir ) ) . toSorted ( ) , [
669+ ".staging-abc" ,
670+ "0.5.0" ,
671+ "0.9.0" ,
672+ "1.0.1" ,
673+ "1.2.0" ,
674+ "1.2.5" ,
675+ "1.3.0" ,
676+ "1.3.5" ,
677+ "1.4.0" ,
678+ "1.5.0" ,
679+ "2.0.0" ,
680+ "other" ,
681+ ] ) ;
682+ } ) ,
683+ ) ;
684+
685+ it . effect ( "matches a Windows shim whatever its casing" , ( ) =>
686+ Effect . gen ( function * ( ) {
687+ const fs = yield * FileSystem . FileSystem ;
688+ const path = yield * Path . Path ;
689+ const { baseDir, versionsDir } = yield * setup ( [ "1.0.0" , "1.1.0" ] ) ;
690+ const shim = path . join ( baseDir , "t3.cmd" ) ;
691+ yield * fs . writeFileString (
692+ shim ,
693+ `@echo off\r\n"${ path . join ( versionsDir , "1.0.0" , "t3.exe" ) . toUpperCase ( ) } " %*` ,
694+ ) ;
695+ yield * fs . writeFileString ( path . join ( baseDir , "runtime" , "launchers" ) , `${ shim } \n` ) ;
696+
697+ const removed = yield * prunePinnedRuntimes ( { baseDir, keep : [ "2.0.0" ] } ) . pipe (
698+ Effect . provideService (
699+ ProcessRunner . ProcessRunner ,
700+ lsofRunner ( "C:\\Windows\\explorer.exe\n" ) ,
701+ ) ,
702+ Effect . provideService ( HostProcess . Platform , "win32" ) ,
703+ Effect . provideService ( HostProcess . Environment , { } ) ,
704+ ) ;
705+ assert . deepEqual ( removed , [ "1.1.0" ] ) ;
706+ } ) ,
707+ ) ;
708+
709+ it . effect . each ( [
710+ { name : "the process list is unavailable" , state : undefined , lsof : "" } ,
711+ { name : "the service state is unparseable" , state : "{" , lsof : "p1\nftxt\nn/sbin/launchd\n" } ,
712+ {
713+ name : "the service state cannot be read" ,
714+ state : null ,
715+ lsof : "p1\nftxt\nn/sbin/launchd\n" ,
716+ } ,
717+ ] ) ( "keeps everything when $name" , ( { state, lsof } ) =>
718+ Effect . gen ( function * ( ) {
719+ const fs = yield * FileSystem . FileSystem ;
720+ const path = yield * Path . Path ;
721+ const { baseDir, versionsDir } = yield * setup ( [ "1.0.0" ] ) ;
722+ const statePath = path . join ( baseDir , "runtime" , "service-state.json" ) ;
723+ if ( state === null ) yield * fs . makeDirectory ( statePath ) ;
724+ else if ( state !== undefined ) yield * fs . writeFileString ( statePath , state ) ;
725+
726+ const removed = yield * prunePinnedRuntimes ( { baseDir, keep : [ "2.0.0" ] } ) . pipe (
727+ Effect . provideService ( ProcessRunner . ProcessRunner , lsofRunner ( lsof ) ) ,
728+ Effect . provideService ( HostProcess . Platform , "darwin" ) ,
729+ Effect . provideService ( HostProcess . Environment , { T3CODE_INSTALL_BIN_DIR : baseDir } ) ,
730+ ) ;
731+ assert . deepEqual ( removed , [ ] ) ;
732+ assert . deepEqual ( yield * fs . readDirectory ( versionsDir ) , [ "1.0.0" ] ) ;
733+ } ) ,
734+ ) ;
735+
736+ it . effect . skipIf ( HostProcess . Platform . defaultValue ( ) !== "linux" ) (
737+ "keeps a runtime a process runs through a symlink" ,
738+ ( ) =>
739+ Effect . gen ( function * ( ) {
740+ const fs = yield * FileSystem . FileSystem ;
741+ const path = yield * Path . Path ;
742+ const { baseDir, versionsDir } = yield * setup ( [ "1.0.0" , "1.1.0" ] ) ;
743+ const entry = path . join ( versionsDir , "1.0.0" , "t3" ) ;
744+ yield * fs . copyFile ( "/bin/sleep" , entry ) ;
745+ yield * fs . chmod ( entry , 0o755 ) ;
746+ const link = path . join ( baseDir , "t3" ) ;
747+ yield * fs . symlink ( entry , link ) ;
748+ // Its argv names only the link, as with `t3 serve` run from PATH. Removing the link after
749+ // spawning leaves the process's executable as the only reference.
750+ const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
751+ yield * spawner . spawn ( ChildProcess . make ( link , [ "30" ] ) ) ;
752+ yield * fs . remove ( link ) ;
753+
754+ const removed = yield * prunePinnedRuntimes ( { baseDir, keep : [ "2.0.0" ] } ) . pipe (
755+ Effect . provideService ( HostProcess . Platform , "linux" ) ,
756+ Effect . provideService ( HostProcess . Environment , { T3CODE_INSTALL_BIN_DIR : baseDir } ) ,
757+ Effect . provide ( ProcessRunner . layer ) ,
758+ ) ;
759+ assert . deepEqual ( removed , [ "1.1.0" ] ) ;
760+ } ) ,
761+ ) ;
762+ } ) ;
0 commit comments