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

Tests: Simplify loadSCMPackage implementation #2852

Merged
merged 1 commit into from
Feb 13, 2024
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
64 changes: 32 additions & 32 deletions source/dub/test/base.d
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,28 @@ public class TestSelectedVersions : SelectedVersions {
*/
package class TestPackageManager : PackageManager
{
/// `loadSCMPackage` will strip some part of the remote / repository,
/// which we need to mimic to provide a usable API.
private struct GitReference {
///
this (in Repository repo) {
this.remote = repo.remote.chompPrefix("git+");
this.ref_ = repo.ref_.chompPrefix("~");
}

///
this (in string remote, in string gitref) {
this.remote = remote;
this.ref_ = gitref;
}

string remote;
string ref_;
}


/// List of all SCM packages that can be fetched by this instance
protected Package[Repository] scm;
protected string[GitReference] scm;
/// The virtual filesystem that this PackageManager acts on
protected FSEntry fs;

Expand All @@ -294,7 +314,6 @@ package class TestPackageManager : PackageManager
}

// Re-introduce hidden/deprecated overloads
public alias loadSCMPackage = PackageManager.loadSCMPackage;
public alias store = PackageManager.store;

/// Disabled as semantic are not implementable unless a virtual FS is created
Expand Down Expand Up @@ -374,38 +393,19 @@ package class TestPackageManager : PackageManager
}

/**
* Re-Implementation of `loadSCMPackage`.
* Re-Implementation of `gitClone`.
*
* The base implementation will do a `git` clone, which we would like to avoid.
* Instead, we allow unittests to explicitly define what packages should be
* reachable in a given test.
* The base implementation will do a `git` clone, to the file-system.
* We need to mock both the `git` part and the write to the file system.
*/
public override Package loadSCMPackage(in PackageName name, in Repository repo)
{
import std.string : chompPrefix;

// We're trying to match `loadGitPackage` as much as possible
if (!repo.ref_.startsWith("~") && !repo.ref_.isGitHash)
return null;

string gitReference = repo.ref_.chompPrefix("~");
NativePath destination = this.getPackagePath(PlacementLocation.user, name, repo.ref_);

foreach (p; getPackageIterator(name.toString()))
if (p.path == destination)
return p;

return this.loadSCMRepository(name, repo);
}

/// The private part of `loadSCMPackage`
protected Package loadSCMRepository(in PackageName name, in Repository repo)
protected override bool gitClone(string remote, string gitref, in NativePath dest)
{
if (auto prepo = repo in this.scm) {
this.addPackages(this.m_internal.fromPath, *prepo);
return *prepo;
if (auto pstr = GitReference(remote, gitref) in this.scm) {
this.fs.mkdir(dest);
this.fs.writeFile(dest ~ "dub.json", *pstr);
return true;
}
return null;
return false;
}

/**
Expand Down Expand Up @@ -443,9 +443,9 @@ package class TestPackageManager : PackageManager
}

/// Add a reachable SCM package to this `PackageManager`
public void addTestSCMPackage(Repository repo, Package pkg)
public void addTestSCMPackage(in Repository repo, string dub_json)
{
this.scm[repo] = pkg;
this.scm[GitReference(repo)] = dub_json;
}

///
Expand Down
5 changes: 1 addition & 4 deletions source/dub/test/other.d
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ unittest

scope dub = new TestDub();
dub.packageManager.addTestSCMPackage(
Repository(ValidURL, ValidHash),
// Note: SCM package are always marked as using `~master`
dub.makeTestPackage(`{ "name": "dep1" }`, Version(`~master`)),
);
Repository(ValidURL, ValidHash), `{ "name": "dep1" }`);

// Invalid URL, valid hash
const a = Template.format("a", "git+https://nope.nope", ValidHash);
Expand Down
Loading