Skip to content

Commit 816cea7

Browse files
authored
[path] Fix test race condition caused by changing Directory.current (#890)
1 parent de09be6 commit 816cea7

File tree

1 file changed

+59
-59
lines changed

1 file changed

+59
-59
lines changed

pkgs/path/test/io_test.dart

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -43,81 +43,81 @@ void main() {
4343

4444
test(
4545
'uses the previous working directory if deleted',
46-
() {
47-
final dir = io.Directory.current.path;
48-
try {
49-
final temp = io.Directory.systemTemp.createTempSync('path_test');
50-
final tempPath = temp.resolveSymbolicLinksSync();
51-
io.Directory.current = temp;
52-
53-
// Call "current" once so that it can be cached.
54-
expect(path.normalize(path.absolute(path.current)), equals(tempPath));
55-
56-
temp.deleteSync();
57-
58-
// Even though the directory no longer exists, no exception is thrown.
59-
expect(path.normalize(path.absolute(path.current)), equals(tempPath));
60-
} finally {
61-
io.Directory.current = dir;
62-
}
63-
},
46+
withLocalCurrentDirectory(() {
47+
final temp = io.Directory.systemTemp.createTempSync('path_test');
48+
final tempPath = temp.resolveSymbolicLinksSync();
49+
io.Directory.current = temp;
50+
51+
// Call "current" once so that it can be cached.
52+
expect(path.normalize(path.absolute(path.current)), equals(tempPath));
53+
54+
temp.deleteSync();
55+
56+
// Even though the directory no longer exists, no exception is thrown.
57+
expect(path.normalize(path.absolute(path.current)), equals(tempPath));
58+
}),
6459
//TODO: Figure out why this is failing on windows and fix!
6560
skip: io.Platform.isWindows ? 'Untriaged failure on Windows' : false,
6661
);
6762
});
6863

69-
test('registers changes to the working directory', () {
64+
test('registers changes to the working directory',
65+
withLocalCurrentDirectory(() {
7066
final dir = io.Directory.current.path;
71-
try {
72-
expect(path.absolute('foo/bar'), equals(path.join(dir, 'foo/bar')));
67+
expect(path.absolute('foo/bar'), equals(path.join(dir, 'foo/bar')));
68+
expect(
69+
path.absolute('foo/bar'),
70+
equals(path.context.join(dir, 'foo/bar')),
71+
);
72+
73+
io.Directory.current = path.dirname(dir);
74+
expect(
75+
path.normalize(path.absolute('foo/bar')),
76+
equals(path.normalize(path.join(dir, '../foo/bar'))),
77+
);
78+
expect(
79+
path.normalize(path.absolute('foo/bar')),
80+
equals(path.normalize(path.context.join(dir, '../foo/bar'))),
81+
);
82+
}));
83+
84+
// Regression test for #35. This tests against the *actual* working directory
85+
// rather than just a custom context because we do some processing in
86+
// [path.current] that has clobbered the root in the past.
87+
test(
88+
'absolute works on root working directory',
89+
withLocalCurrentDirectory(() {
90+
io.Directory.current = path.rootPrefix(path.current);
91+
7392
expect(
74-
path.absolute('foo/bar'),
75-
equals(path.context.join(dir, 'foo/bar')),
93+
path.relative(path.absolute('foo/bar'), from: path.current),
94+
path.relative(path.absolute('foo/bar')),
7695
);
7796

78-
io.Directory.current = path.dirname(dir);
7997
expect(
8098
path.normalize(path.absolute('foo/bar')),
81-
equals(path.normalize(path.join(dir, '../foo/bar'))),
99+
equals(path.normalize(path.join(path.current, '../foo/bar'))),
82100
);
101+
83102
expect(
84103
path.normalize(path.absolute('foo/bar')),
85-
equals(path.normalize(path.context.join(dir, '../foo/bar'))),
104+
equals(path.normalize(path.join(path.current, '../foo/bar'))),
86105
);
87-
} finally {
88-
io.Directory.current = dir;
89-
}
90-
});
91-
92-
// Regression test for #35. This tests against the *actual* working directory
93-
// rather than just a custom context because we do some processing in
94-
// [path.current] that has clobbered the root in the past.
95-
test(
96-
'absolute works on root working directory',
97-
() {
98-
final dir = path.current;
99-
try {
100-
io.Directory.current = path.rootPrefix(path.current);
101-
102-
expect(
103-
path.relative(path.absolute('foo/bar'), from: path.current),
104-
path.relative(path.absolute('foo/bar')),
105-
);
106-
107-
expect(
108-
path.normalize(path.absolute('foo/bar')),
109-
equals(path.normalize(path.join(path.current, '../foo/bar'))),
110-
);
111-
112-
expect(
113-
path.normalize(path.absolute('foo/bar')),
114-
equals(path.normalize(path.join(path.current, '../foo/bar'))),
115-
);
116-
} finally {
117-
io.Directory.current = dir;
118-
}
119-
},
106+
}),
120107
//TODO(kevmoo): figure out why this is failing on windows and fix!
121108
skip: io.Platform.isWindows ? 'Untriaged failure on Windows' : null,
122109
);
123110
}
111+
112+
/// Runs [body] in a zone with a local current working directory.
113+
///
114+
/// Avoids clobbering the current working directory of the entire process
115+
/// when writing to it and reading it back through `dart:io` functions.
116+
R Function() withLocalCurrentDirectory<R>(R Function() body) {
117+
var savedCurrentDirectory = io.Directory.current;
118+
return () => io.IOOverrides.runZoned(body,
119+
getCurrentDirectory: () => savedCurrentDirectory,
120+
setCurrentDirectory: (dir) {
121+
savedCurrentDirectory = io.Directory(dir);
122+
});
123+
}

0 commit comments

Comments
 (0)