@@ -43,81 +43,81 @@ void main() {
43
43
44
44
test (
45
45
'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
+ }),
64
59
//TODO: Figure out why this is failing on windows and fix!
65
60
skip: io.Platform .isWindows ? 'Untriaged failure on Windows' : false ,
66
61
);
67
62
});
68
63
69
- test ('registers changes to the working directory' , () {
64
+ test ('registers changes to the working directory' ,
65
+ withLocalCurrentDirectory (() {
70
66
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
+
73
92
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' )),
76
95
);
77
96
78
- io.Directory .current = path.dirname (dir);
79
97
expect (
80
98
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' ))),
82
100
);
101
+
83
102
expect (
84
103
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' ))),
86
105
);
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
+ }),
120
107
//TODO(kevmoo): figure out why this is failing on windows and fix!
121
108
skip: io.Platform .isWindows ? 'Untriaged failure on Windows' : null ,
122
109
);
123
110
}
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