@@ -10,8 +10,10 @@ const ROOT = process.cwd();
1010
1111function makeFixture ( ) {
1212 const tmpDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "v8s-detach-" ) ) ;
13+ const sourceDir = path . join ( tmpDir , "source" ) ;
14+ const fixtureDir = path . join ( tmpDir , "fixture" ) ;
1315
14- fs . cpSync ( ROOT , tmpDir , {
16+ fs . cpSync ( ROOT , sourceDir , {
1517 recursive : true ,
1618 filter : ( sourcePath ) => {
1719 const relative = path . relative ( ROOT , sourcePath ) ;
@@ -20,18 +22,39 @@ function makeFixture() {
2022 }
2123 } ) ;
2224
23- fs . mkdirSync ( path . join ( tmpDir , ".git" ) , { recursive : true } ) ;
24- fs . mkdirSync ( path . join ( tmpDir , ".github" , "workflows" ) , { recursive : true } ) ;
25- fs . writeFileSync ( path . join ( tmpDir , "RELEASE.md" ) , "release notes\n" ) ;
26- fs . writeFileSync ( path . join ( tmpDir , "scripts" , "v8s.zsh" ) , "# legacy helper\n" ) ;
25+ fs . mkdirSync ( path . join ( sourceDir , ".github" , "workflows" ) , { recursive : true } ) ;
26+ fs . writeFileSync ( path . join ( sourceDir , "RELEASE.md" ) , "release notes\n" ) ;
27+ fs . writeFileSync ( path . join ( sourceDir , "scripts" , "v8s.zsh" ) , "# legacy helper\n" ) ;
2728
28- return tmpDir ;
29+ git ( [ "init" ] , sourceDir ) ;
30+ git ( [ "config" , "user.email" , "tests@example.com" ] , sourceDir ) ;
31+ git ( [ "config" , "user.name" , "vanityURLs tests" ] , sourceDir ) ;
32+ git ( [ "add" , "." ] , sourceDir ) ;
33+ git ( [ "commit" , "-m" , "test fixture release" ] , sourceDir ) ;
34+ git ( [ "tag" , "v9.9.9" ] , sourceDir ) ;
35+ fs . writeFileSync ( path . join ( sourceDir , "CURRENT_ONLY.txt" ) , "current branch only\n" ) ;
36+ git ( [ "add" , "CURRENT_ONLY.txt" ] , sourceDir ) ;
37+ git ( [ "commit" , "-m" , "test fixture current branch" ] , sourceDir ) ;
38+
39+ execFileSync ( "git" , [ "clone" , sourceDir , fixtureDir ] , {
40+ stdio : "pipe"
41+ } ) ;
42+
43+ return fixtureDir ;
2944}
3045
3146function exists ( fixture , relativePath ) {
3247 return fs . existsSync ( path . join ( fixture , relativePath ) ) ;
3348}
3449
50+ function git ( args , cwd , options = { } ) {
51+ return execFileSync ( "git" , args , {
52+ cwd,
53+ encoding : "utf8" ,
54+ stdio : options . capture ? [ "ignore" , "pipe" , "pipe" ] : "pipe"
55+ } ) . trim ( ) ;
56+ }
57+
3558{
3659 const fixture = makeFixture ( ) ;
3760 const instanceReadmePath = path . join ( fixture , "docs" , "README.md" ) ;
@@ -44,9 +67,13 @@ function exists(fixture, relativePath) {
4467 } ) ;
4568
4669 assert . equal ( fs . readFileSync ( path . join ( fixture , "README.md" ) , "utf8" ) , instanceReadme || originalReadme ) ;
70+ assert . equal (
71+ exists ( fixture , "CURRENT_ONLY.txt" ) ,
72+ false ,
73+ "detach should switch to the latest stable release by default"
74+ ) ;
4775
4876 for ( const relativePath of [
49- ".git" ,
5077 ".github" ,
5178 ".all-contributorsrc" ,
5279 ".release-please-manifest.json" ,
@@ -66,6 +93,21 @@ function exists(fixture, relativePath) {
6693 assert . equal ( exists ( fixture , "package.json" ) , true ) ;
6794 assert . equal ( exists ( fixture , "scripts/v8s.sh" ) , true ) ;
6895 assert . equal ( exists ( fixture , "scripts/v8s-lnk" ) , true ) ;
96+ assert . equal ( exists ( fixture , ".git" ) , true , "detach should initialize a fresh instance Git repository" ) ;
97+ assert . equal ( git ( [ "rev-parse" , "--is-inside-work-tree" ] , fixture , { capture : true } ) , "true" ) ;
98+ assert . throws ( ( ) => git ( [ "config" , "--get" , "remote.origin.url" ] , fixture , { capture : true } ) ) ;
99+ }
100+
101+ {
102+ const fixture = makeFixture ( ) ;
103+
104+ execFileSync ( process . execPath , [ "scripts/detach-instance.mjs" , "--current-ref" ] , {
105+ cwd : fixture ,
106+ stdio : "pipe"
107+ } ) ;
108+
109+ assert . equal ( exists ( fixture , "CURRENT_ONLY.txt" ) , true , "--current-ref should detach the current checkout" ) ;
110+ assert . equal ( exists ( fixture , ".git" ) , true , "--current-ref should still initialize a fresh instance Git repository" ) ;
69111}
70112
71113console . log ( "detach tests ok" ) ;
0 commit comments