Skip to content

Commit 4b734dc

Browse files
committed
[path] Fix test race condition caused by changing Directory.current
1 parent de09be6 commit 4b734dc

File tree

1 file changed

+56
-59
lines changed

1 file changed

+56
-59
lines changed

pkgs/path/test/io_test.dart

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -43,81 +43,78 @@ 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+
currentDirHelper(() {
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', currentDirHelper(() {
7065
final dir = io.Directory.current.path;
71-
try {
72-
expect(path.absolute('foo/bar'), equals(path.join(dir, 'foo/bar')));
66+
expect(path.absolute('foo/bar'), equals(path.join(dir, 'foo/bar')));
67+
expect(
68+
path.absolute('foo/bar'),
69+
equals(path.context.join(dir, 'foo/bar')),
70+
);
71+
72+
io.Directory.current = path.dirname(dir);
73+
expect(
74+
path.normalize(path.absolute('foo/bar')),
75+
equals(path.normalize(path.join(dir, '../foo/bar'))),
76+
);
77+
expect(
78+
path.normalize(path.absolute('foo/bar')),
79+
equals(path.normalize(path.context.join(dir, '../foo/bar'))),
80+
);
81+
}));
82+
83+
// Regression test for #35. This tests against the *actual* working directory
84+
// rather than just a custom context because we do some processing in
85+
// [path.current] that has clobbered the root in the past.
86+
test(
87+
'absolute works on root working directory',
88+
currentDirHelper(() {
89+
io.sleep(const Duration(seconds: 2));
90+
io.Directory.current = path.rootPrefix(path.current);
91+
io.sleep(const Duration(seconds: 2));
92+
7393
expect(
74-
path.absolute('foo/bar'),
75-
equals(path.context.join(dir, 'foo/bar')),
94+
path.relative(path.absolute('foo/bar'), from: path.current),
95+
path.relative(path.absolute('foo/bar')),
7696
);
7797

78-
io.Directory.current = path.dirname(dir);
7998
expect(
8099
path.normalize(path.absolute('foo/bar')),
81-
equals(path.normalize(path.join(dir, '../foo/bar'))),
100+
equals(path.normalize(path.join(path.current, '../foo/bar'))),
82101
);
102+
83103
expect(
84104
path.normalize(path.absolute('foo/bar')),
85-
equals(path.normalize(path.context.join(dir, '../foo/bar'))),
105+
equals(path.normalize(path.join(path.current, '../foo/bar'))),
86106
);
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-
},
107+
}),
120108
//TODO(kevmoo): figure out why this is failing on windows and fix!
121109
skip: io.Platform.isWindows ? 'Untriaged failure on Windows' : null,
122110
);
123111
}
112+
113+
dynamic Function() currentDirHelper(dynamic Function() body) {
114+
var savedCurrentDirectory = io.Directory.current;
115+
return () => io.IOOverrides.runZoned(body,
116+
getCurrentDirectory: () => savedCurrentDirectory,
117+
setCurrentDirectory: (dir) {
118+
savedCurrentDirectory = io.Directory(dir);
119+
});
120+
}

0 commit comments

Comments
 (0)