Skip to content

Commit

Permalink
Show warning when using deps in the wrong folder (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giuspepe committed Jun 20, 2023
1 parent a62b3fc commit e6b1a7e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
22 changes: 22 additions & 0 deletions sidekick_core/lib/src/commands/deps_command.dart
Expand Up @@ -60,11 +60,13 @@ class DepsCommand extends Command {
throw "Package with name $packageName not found in "
"${SidekickContext.projectRoot.path}";
}
_warnIfNotInProject();
// only get deps for selected package
_getDependencies(package);
return;
}

_warnIfNotInProject();
final errorBuffer = StringBuffer();

final globExcludes = excludeGlob
Expand Down Expand Up @@ -114,4 +116,24 @@ class DepsCommand extends Command {
);
print("\n");
}

void _warnIfNotInProject() {
final currentDir = Directory.current;
final projectRoot = SidekickContext.projectRoot;
if (!currentDir.isWithinOrEqual(projectRoot)) {
printerr("Warning: You aren't getting the dependencies of the current "
"working directory, but of project '${SidekickContext.cliName}'.");
}
}
}

extension on Directory {
bool isWithinOrEqual(Directory dir) {
return this.isWithin(dir) ||
// canonicalize is necessary, otherwise '/a/b/c' != '/a/b/c/' != '/a/b/c/.' != '/a/b/c/../c'
dir.canonicalized.path == canonicalized.path;
}

/// A [Directory] whose path is the canonicalized path of [this].
Directory get canonicalized => Directory(canonicalize(path));
}
30 changes: 30 additions & 0 deletions sidekick_core/test/deps_command_test.dart
Expand Up @@ -217,4 +217,34 @@ environment:
);
});
});

test('print warning when getting deps outside of project', () async {
await insideFakeProjectWithSidekick((dir) async {
final fakeStdErr = FakeStdoutStream();
await overrideIoStreams(
stderr: () => fakeStdErr,
body: () async {
setUpPackages(dir);

// move to a different dir
final otherDir = Directory.systemTemp.createTempSync('');
addTearDown(() => otherDir.deleteSync());
IOOverrides.current!.setCurrentDirectory(otherDir.path);

final runner = initializeSidekick(dartSdkPath: systemDartSdkPath());

runner.addCommand(DepsCommand());
await runner.run(['deps']);

expect(exitCode, 0);
expect(
fakeStdErr.lines,
contains(
"Warning: You aren't getting the dependencies of the current "
"working directory, but of project 'dash'."),
);
},
);
});
});
}

0 comments on commit e6b1a7e

Please sign in to comment.