Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2051: Running unit tests from DUB single file packages fails #2052

Merged
merged 1 commit into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ jobs:
# The value doesn't matter as long as it's > 2.087
FRONTEND: 2.093.0
run: |
dub build --compiler=${{ env.DC }}
dub run --compiler=${{ env.DC }} --single test/issue_2051_running_unittests_from_dub_single_file_packages_fails.d
./scripts/ci/travis.sh

- name: '[Windows] Test'
Expand All @@ -72,3 +74,4 @@ jobs:
run: |
dub build --compiler=${{ env.DC }}
dub test --compiler=${{ env.DC }}
dub run --compiler=${{ env.DC }} --single test\issue_2051_running_unittests_from_dub_single_file_packages_fails.d
3 changes: 3 additions & 0 deletions changelog/fix-2051.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix #2051 "Running unit tests from DUB single file packages fails"

Now dub is capable run test command in single mode like: `dub test --single yoursinglefile.d`
5 changes: 4 additions & 1 deletion source/dub/dub.d
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,10 @@ class Dub {
foreach (file; lbuildsettings.sourceFiles) {
if (file.endsWith(".d")) {
auto fname = NativePath(file).head.name;
if (NativePath(file).relativeTo(m_project.rootPackage.path) == NativePath(mainfil)) {
NativePath msf = NativePath(mainfil);
if (msf.absolute)
msf = msf.relativeTo(m_project.rootPackage.path);
if (NativePath(file).relativeTo(m_project.rootPackage.path) == msf) {
logWarn("Excluding main source file %s from test.", mainfil);
tcinfo.excludedSourceFiles[""] ~= mainfil;
continue;
Expand Down
1 change: 1 addition & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ version-spec/**/foo.cmake
version-spec/**/foo

/test_registry
/issue_2051_running_unittests_from_dub_single_file_packages_fails
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/+ dub.sdl:
name "issue_2051_running_unittests_from_dub_single_file_packages_fails"
+/

import std.algorithm : any;
import std.conv : text;
import std.file : tempDir;
import std.stdio : File, writeln;
import std.string : lineSplitter;
import std.path : buildPath;
import std.process : environment, executeShell;

auto executeCommand(string command)
{
import std.exception : enforce;

auto dub = executeShell(command);
writeln("--- dub output:");
foreach(line; dub.output.lineSplitter)
writeln("\t", line);
writeln("--- end of dub output");

enforce(dub.status == 0, "couldn't build the project, see above");

return dub.output;
}

/// check dub output to determine rebuild has not been triggered
auto checkUnittestsResult(string output)
{
if (output.lineSplitter.any!(a=> a == "All unit tests have been run successfully."))
{
writeln("\nOk. Unittest passed.");
return 0;
}
else
{
writeln("\nError. Unittests failed.");
return 1;
}
}

int main()
{
auto dub = environment.get("DUB");
if (!dub.length)
dub = buildPath(".", "bin", "dub");

string filename;
// create test_project
{
filename = tempDir.buildPath("issue_2051.d");
auto f = File(filename, "w");
f.write(
`#!/usr/bin/env dub
/+ dub.sdl:
name "issue2051"
+/

version(unittest) {}
else void main()
{
}

unittest
{
auto input = [1721];
assert(input[0] == 1721);
}
` );
}

return text(dub, " test --single ", filename)
.executeCommand
.checkUnittestsResult;
}