|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:path/path.dart' as p; |
| 8 | +import 'package:test/test.dart'; |
| 9 | + |
| 10 | +import 'descriptor.dart' as d; |
| 11 | + |
| 12 | +/// Describes a symlink. |
| 13 | +class LinkDescriptor extends d.Descriptor { |
| 14 | + /// On windows symlinks to directories are distinct from symlinks to files. |
| 15 | + final bool forceDirectory; |
| 16 | + final String target; |
| 17 | + LinkDescriptor(super.name, this.target, {this.forceDirectory = false}); |
| 18 | + |
| 19 | + @override |
| 20 | + Future<void> create([String? parent]) async { |
| 21 | + final path = p.join(parent ?? d.sandbox, name); |
| 22 | + if (forceDirectory) { |
| 23 | + if (Platform.isWindows) { |
| 24 | + Process.runSync('cmd', ['/c', 'mklink', '/D', path, target]); |
| 25 | + } else { |
| 26 | + Link(path).createSync(target); |
| 27 | + } |
| 28 | + } else { |
| 29 | + Link(path).createSync(target); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + @override |
| 34 | + String describe() { |
| 35 | + return 'symlink at $name targeting $target'; |
| 36 | + } |
| 37 | + |
| 38 | + @override |
| 39 | + Future<void> validate([String? parent]) async { |
| 40 | + final link = Link(p.join(parent ?? d.sandbox, name)); |
| 41 | + try { |
| 42 | + final actualTarget = link.targetSync(); |
| 43 | + expect( |
| 44 | + actualTarget, |
| 45 | + target, |
| 46 | + reason: 'Link doesn\'t point where expected.', |
| 47 | + ); |
| 48 | + } on FileSystemException catch (e) { |
| 49 | + fail('Could not read link at $name $e'); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +d.Descriptor link(String name, String target, {bool forceDirectory = false}) { |
| 55 | + return LinkDescriptor(name, target, forceDirectory: forceDirectory); |
| 56 | +} |
0 commit comments