Skip to content

Commit 5f4ea8e

Browse files
committed
Tests: Simplify loadSCMPackage implementation
Now that we can override the git clone operation and load from the virtual filesystem, we can remove a lot of the duplicated logic. This is currently hardcoded to a dub.json, but future iterations can store an FSEntry representing the full package.
1 parent 0907b37 commit 5f4ea8e

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

source/dub/test/base.d

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,28 @@ public class TestSelectedVersions : SelectedVersions {
299299
*/
300300
package class TestPackageManager : PackageManager
301301
{
302+
/// `loadSCMPackage` will strip some part of the remote / repository,
303+
/// which we need to mimic to provide a usable API.
304+
private struct GitReference {
305+
///
306+
this (in Repository repo) {
307+
this.remote = repo.remote.chompPrefix("git+");
308+
this.ref_ = repo.ref_.chompPrefix("~");
309+
}
310+
311+
///
312+
this (in string remote, in string gitref) {
313+
this.remote = remote;
314+
this.ref_ = gitref;
315+
}
316+
317+
string remote;
318+
string ref_;
319+
}
320+
321+
302322
/// List of all SCM packages that can be fetched by this instance
303-
protected Package[Repository] scm;
323+
protected string[GitReference] scm;
304324
/// The virtual filesystem that this PackageManager acts on
305325
protected FSEntry fs;
306326

@@ -314,7 +334,6 @@ package class TestPackageManager : PackageManager
314334
}
315335

316336
// Re-introduce hidden/deprecated overloads
317-
public alias loadSCMPackage = PackageManager.loadSCMPackage;
318337
public alias store = PackageManager.store;
319338

320339
/// Disabled as semantic are not implementable unless a virtual FS is created
@@ -394,38 +413,19 @@ package class TestPackageManager : PackageManager
394413
}
395414

396415
/**
397-
* Re-Implementation of `loadSCMPackage`.
416+
* Re-Implementation of `gitClone`.
398417
*
399-
* The base implementation will do a `git` clone, which we would like to avoid.
400-
* Instead, we allow unittests to explicitly define what packages should be
401-
* reachable in a given test.
418+
* The base implementation will do a `git` clone, to the file-system.
419+
* We need to mock both the `git` part and the write to the file system.
402420
*/
403-
public override Package loadSCMPackage(in PackageName name, in Repository repo)
404-
{
405-
import std.string : chompPrefix;
406-
407-
// We're trying to match `loadGitPackage` as much as possible
408-
if (!repo.ref_.startsWith("~") && !repo.ref_.isGitHash)
409-
return null;
410-
411-
string gitReference = repo.ref_.chompPrefix("~");
412-
NativePath destination = this.getPackagePath(PlacementLocation.user, name, repo.ref_);
413-
414-
foreach (p; getPackageIterator(name.toString()))
415-
if (p.path == destination)
416-
return p;
417-
418-
return this.loadSCMRepository(name, repo);
419-
}
420-
421-
/// The private part of `loadSCMPackage`
422-
protected Package loadSCMRepository(in PackageName name, in Repository repo)
421+
protected override bool gitClone(string remote, string gitref, in NativePath dest)
423422
{
424-
if (auto prepo = repo in this.scm) {
425-
this.addPackages(this.m_internal.fromPath, *prepo);
426-
return *prepo;
423+
if (auto pstr = GitReference(remote, gitref) in this.scm) {
424+
this.fs.mkdir(dest);
425+
this.fs.writeFile(dest ~ "dub.json", *pstr);
426+
return true;
427427
}
428-
return null;
428+
return false;
429429
}
430430

431431
/**
@@ -463,9 +463,9 @@ package class TestPackageManager : PackageManager
463463
}
464464

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

471471
///

source/dub/test/other.d

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ unittest
2323

2424
scope dub = new TestDub();
2525
dub.packageManager.addTestSCMPackage(
26-
Repository(ValidURL, ValidHash),
27-
// Note: SCM package are always marked as using `~master`
28-
dub.makeTestPackage(`{ "name": "dep1" }`, Version(`~master`)),
29-
);
26+
Repository(ValidURL, ValidHash), `{ "name": "dep1" }`);
3027

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

0 commit comments

Comments
 (0)