@@ -2,10 +2,11 @@ import once from "function-once";
22import * as Archive from "json-archive" ;
33import exec from "nanoexec" ;
44import fs from "node:fs/promises" ;
5+ import os from "node:os" ;
56import path from "node:path" ;
67import process from "node:process" ;
7- import { setTimeout as delay } from "node:timers/promises" ;
88import Base64 from "radix64-encoding" ;
9+ import zeptoid from "zeptoid" ;
910
1011import { expect , test } from "@jest/globals" ;
1112import serializerRaw from "jest-snapshot-serializer-raw" ;
@@ -17,12 +18,12 @@ expect.addSnapshotSerializer(serializerAnsi);
1718const ROOT_PATH = process . cwd ( ) ;
1819const BIN_PATH = path . join ( ROOT_PATH , "dist" , "bin.js" ) ;
1920const FIXTURES_PATH = path . join ( ROOT_PATH , "test" , "__fixtures__" ) ;
21+ const TESTS_PATH = path . join ( ROOT_PATH , "test" ) ;
2022
2123async function getArchive ( folderPath ) {
2224 const packPrev = await Archive . pack ( folderPath ) ;
2325 const archive = {
24- getPack : once ( async ( ) => {
25- await delay ( 100 ) ; // Giving a little time for the FS to settle
26+ getPack : once ( ( ) => {
2627 return Archive . pack ( folderPath ) ;
2728 } ) ,
2829 getChanged : once ( async ( ) => {
@@ -65,6 +66,27 @@ function getFixturesPath(dir) {
6566 return path . join ( FIXTURES_PATH , dir ) ;
6667}
6768
69+ async function getIsolatedFixtures ( dir ) {
70+ const [ rootPart , ...subParts ] = dir . split ( "/" ) ;
71+ const fixturesPath = getFixturesPath ( rootPart ) ;
72+ const tempPath = await getTempPath ( `prettier-${ rootPart } ` ) ;
73+ const tempGitPath = path . join ( tempPath , ".git" ) ;
74+ const isolatedPath = path . join ( tempPath , ...subParts ) ;
75+
76+ await fs . mkdir ( tempPath , { recursive : true } ) ;
77+ await fs . mkdir ( tempGitPath , { recursive : true } ) ;
78+ await fs . cp ( fixturesPath , tempPath , { recursive : true } ) ;
79+
80+ const dispose = ( ) => {
81+ return fs . rm ( tempPath , { recursive : true , force : true } ) ;
82+ } ;
83+
84+ return {
85+ path : isolatedPath ,
86+ dispose,
87+ } ;
88+ }
89+
6890function getNormalizedOutput ( output , options ) {
6991 // \r is trimmed from jest snapshots by default;
7092 // manually replacing this character with /*CR*/ to test its true presence
@@ -74,9 +96,16 @@ function getNormalizedOutput(output, options) {
7496 return output ;
7597}
7698
99+ async function getTempPath ( prefix ) {
100+ const rootPath = await fs . realpath ( os . tmpdir ( ) ) ;
101+ const tempPath = path . join ( rootPath , `${ prefix } -${ zeptoid ( ) } ` ) ;
102+ return tempPath ;
103+ }
104+
77105async function runCommand ( dir , args , options ) {
78- const cwd = getFixturesPath ( dir ) ;
79- const archive = dir ? await getArchive ( cwd ) : undefined ;
106+ const fixtures = dir ? await getIsolatedFixtures ( dir ) : undefined ;
107+ const archive = fixtures ? await getArchive ( fixtures . path ) : undefined ;
108+ const cwd = fixtures ? fixtures . path : TESTS_PATH ;
80109 const argsWithReplacements = args . map ( ( arg ) => arg . replaceAll ( "$CWD" , cwd ) ) ;
81110 const result = exec ( "node" , [ BIN_PATH , ...argsWithReplacements ] , { cwd, stdio : "pipe" } ) ;
82111
@@ -90,7 +119,7 @@ async function runCommand(dir, args, options) {
90119 const stderr = getNormalizedOutput ( ( await result . stderr ) . toString ( ) ) ;
91120 const write = ( await archive ?. getDiff ( ) ) || [ ] ;
92121
93- await archive ?. reset ( ) ;
122+ await fixtures ?. dispose ( ) ;
94123
95124 return { status, stdout, stderr, write } ;
96125}
0 commit comments