Skip to content

Commit

Permalink
Remove most dependency to std.file and dub.internal.core.file from Dub
Browse files Browse the repository at this point in the history
By using the Filesystem instance we can start writing better tests,
in this case getting the ability to load configurations from tests.
  • Loading branch information
Geod24 committed Jun 25, 2024
1 parent 94f8243 commit d840503
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions source/dub/dub.d
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import dub.dependency;
import dub.dependencyresolver;
import dub.internal.io.realfs;
import dub.internal.utils;
import dub.internal.vibecompat.core.file;
import dub.internal.vibecompat.data.json;
import dub.internal.vibecompat.inet.url;
import dub.internal.logging;
Expand All @@ -29,7 +28,7 @@ import std.array : array, replace;
import std.conv : text, to;
import std.encoding : sanitize;
import std.exception : enforce;
import std.file;
import std.file : tempDir, thisExePath;
import std.process : environment;
import std.range : assumeSorted, empty;
import std.string;
Expand Down Expand Up @@ -165,7 +164,7 @@ class Dub {
{
this.fs = fs;
m_rootPath = NativePath(root_path);
if (!m_rootPath.absolute) m_rootPath = getWorkingDirectory() ~ m_rootPath;
if (!m_rootPath.absolute) m_rootPath = fs.getcwd() ~ m_rootPath;

init();

Expand Down Expand Up @@ -240,7 +239,7 @@ class Dub {

protected void init()
{
this.m_dirs = SpecialDirs.make();
this.m_dirs = SpecialDirs.make(this.fs);
this.m_config = this.loadConfig(this.m_dirs);
this.m_defaultCompiler = this.determineDefaultCompiler();
}
Expand All @@ -266,13 +265,13 @@ class Dub {
{
import dub.internal.configy.Read;

static void readSettingsFile (NativePath path_, ref Settings current)
static void readSettingsFile (in Filesystem fs, NativePath path_, ref Settings current)
{
// TODO: Remove `StrictMode.Warn` after v1.40 release
// The default is to error, but as the previous parser wasn't
// complaining, we should first warn the user.
const path = path_.toNativeString();
if (path.exists) {
if (fs.existsFile(path_)) {
auto newConf = parseConfigFileSimple!Settings(path, StrictMode.Warn);
if (!newConf.isNull())
current = current.merge(newConf.get());
Expand Down Expand Up @@ -302,11 +301,11 @@ class Dub {
}
}

readSettingsFile(dirs.systemSettings ~ "settings.json", result);
readSettingsFile(dubFolderPath ~ "../etc/dub/settings.json", result);
readSettingsFile(this.fs, dirs.systemSettings ~ "settings.json", result);
readSettingsFile(this.fs, dubFolderPath ~ "../etc/dub/settings.json", result);
version (Posix) {
if (dubFolderPath.absolute && dubFolderPath.startsWith(NativePath("usr")))
readSettingsFile(NativePath("/etc/dub/settings.json"), result);
readSettingsFile(this.fs, NativePath("/etc/dub/settings.json"), result);
}

// Override user + local package path from system / binary settings
Expand All @@ -320,11 +319,11 @@ class Dub {
}

// load user config:
readSettingsFile(dirs.userSettings ~ "settings.json", result);
readSettingsFile(this.fs, dirs.userSettings ~ "settings.json", result);

// load per-package config:
if (!this.m_rootPath.empty)
readSettingsFile(this.m_rootPath ~ "dub.settings.json", result);
readSettingsFile(this.fs, this.m_rootPath ~ "dub.settings.json", result);

// same as userSettings above, but taking into account the
// config loaded from user settings and per-package config as well.
Expand Down Expand Up @@ -457,7 +456,7 @@ class Dub {
@property void rootPath(NativePath root_path)
{
m_rootPath = root_path;
if (!m_rootPath.absolute) m_rootPath = getWorkingDirectory() ~ m_rootPath;
if (!m_rootPath.absolute) m_rootPath = this.fs.getcwd() ~ m_rootPath;
}

/// Returns the name listed in the dub.json of the current
Expand Down Expand Up @@ -572,12 +571,11 @@ class Dub {
void loadSingleFilePackage(NativePath path)
{
import dub.recipe.io : parsePackageRecipe;
import std.file : readText;
import std.path : baseName, stripExtension;

path = makeAbsolute(path);

string file_content = readText(path.toNativeString());
string file_content = this.fs.readText(path);

if (file_content.startsWith("#!")) {
auto idx = file_content.indexOf('\n');
Expand Down Expand Up @@ -840,9 +838,9 @@ class Dub {
}
}

string configFilePath = (m_project.rootPackage.path ~ "dscanner.ini").toNativeString();
if (!args.canFind("--config") && exists(configFilePath)) {
settings.runArgs ~= ["--config", configFilePath];
const configFilePath = (m_project.rootPackage.path ~ "dscanner.ini");
if (!args.canFind("--config") && this.fs.existsFile(configFilePath)) {
settings.runArgs ~= ["--config", configFilePath.toNativeString()];
}

settings.runArgs ~= args ~ [m_project.rootPackage.path.toNativeString()];
Expand Down Expand Up @@ -893,8 +891,7 @@ class Dub {
const cache = this.m_dirs.cache;
logInfo("Cleaning", Color.green, "all artifacts at %s",
cache.toNativeString().color(Mode.bold));
if (existsFile(cache))
rmdirRecurse(cache.toNativeString());
this.fs.removeDir(cache, true);
}

/// Ditto
Expand All @@ -904,10 +901,8 @@ class Dub {
logInfo("Cleaning", Color.green, "artifacts for package %s at %s",
pack.name.color(Mode.bold),
cache.toNativeString().color(Mode.bold));

// TODO: clear target files and copy files
if (existsFile(cache))
rmdirRecurse(cache.toNativeString());
this.fs.removeDir(cache, true);
}

deprecated("Use the overload that accepts either a `Version` or a `VersionRange` as second argument")
Expand Down Expand Up @@ -1477,7 +1472,7 @@ class Dub {
}

writePackageRecipe(srcfile.parentPath ~ ("dub."~destination_file_ext), m_project.rootPackage.rawRecipe);
removeFile(srcfile);
this.fs.removeFile(srcfile);
}

/** Runs DDOX to generate or serve documentation.
Expand Down Expand Up @@ -1607,7 +1602,6 @@ class Dub {
*/
protected string determineDefaultCompiler() const
{
import std.file : thisExePath;
import std.path : buildPath, dirName, expandTilde, isAbsolute, isDirSeparator;
import std.range : front;

Expand Down Expand Up @@ -1637,23 +1631,24 @@ class Dub {
if (result.length)
{
string compilerPath = buildPath(thisExePath().dirName(), result ~ exe);
if (existsFile(compilerPath))
if (this.fs.existsFile(NativePath(compilerPath)))
return compilerPath;
}
else
{
auto nextFound = compilers.find!(bin => existsFile(buildPath(thisExePath().dirName(), bin ~ exe)));
auto nextFound = compilers.find!(
bin => this.fs.existsFile(NativePath(buildPath(thisExePath().dirName(), bin ~ exe))));
if (!nextFound.empty)
return buildPath(thisExePath().dirName(), nextFound.front ~ exe);
}

// If nothing found next to dub, search the user's PATH, starting
// with the compiler name from their DUB config file, if specified.
auto paths = environment.get("PATH", "").splitter(sep).map!NativePath;
if (result.length && paths.canFind!(p => existsFile(p ~ (result ~ exe))))
if (result.length && paths.canFind!(p => this.fs.existsFile(p ~ (result ~ exe))))
return result;
foreach (p; paths) {
auto res = compilers.find!(bin => existsFile(p ~ (bin~exe)));
auto res = compilers.find!(bin => this.fs.existsFile(p ~ (bin~exe)));
if (!res.empty)
return res.front;
}
Expand All @@ -1667,7 +1662,7 @@ class Dub {
import dub.test.base : TestDub;

auto dub = new TestDub(null, ".", null, SkipPackageSuppliers.configured);
immutable testdir = getWorkingDirectory() ~ "test-determineDefaultCompiler";
immutable testdir = dub.fs.getcwd() ~ "test-determineDefaultCompiler";

immutable olddc = environment.get("DC", null);
immutable oldpath = environment.get("PATH", null);
Expand All @@ -1680,19 +1675,18 @@ class Dub {
}
scope (exit) repairenv("DC", olddc);
scope (exit) repairenv("PATH", oldpath);
scope (exit) std.file.rmdirRecurse(testdir.toNativeString());

version (Windows) enum sep = ";", exe = ".exe";
version (Posix) enum sep = ":", exe = "";

immutable dmdpath = testdir ~ "dmd" ~ "bin";
immutable ldcpath = testdir ~ "ldc" ~ "bin";
ensureDirectory(dmdpath);
ensureDirectory(ldcpath);
dub.fs.mkdir(dmdpath);
dub.fs.mkdir(ldcpath);
immutable dmdbin = dmdpath ~ ("dmd" ~ exe);
immutable ldcbin = ldcpath ~ ("ldc2" ~ exe);
writeFile(dmdbin, null);
writeFile(ldcbin, null);
dub.fs.writeFile(dmdbin, "dmd");
dub.fs.writeFile(ldcbin, "ldc");

environment["DC"] = dmdbin.toNativeString();
assert(dub.determineDefaultCompiler() == dmdbin.toNativeString());
Expand Down Expand Up @@ -2066,9 +2060,14 @@ package struct SpecialDirs {
NativePath cache;

/// Returns: An instance of `SpecialDirs` initialized from the environment
deprecated("Use the overload that accepts a `Filesystem`")
public static SpecialDirs make () {
import std.file : tempDir;
scope fs = new RealFS();
return SpecialDirs.make(fs);
}

/// Ditto
public static SpecialDirs make (scope Filesystem fs) {
SpecialDirs result;
result.temp = NativePath(tempDir);

Expand All @@ -2082,7 +2081,7 @@ package struct SpecialDirs {
result.systemSettings = NativePath("/var/lib/dub/");
result.userSettings = NativePath(environment.get("HOME")) ~ ".dub/";
if (!result.userSettings.absolute)
result.userSettings = getWorkingDirectory() ~ result.userSettings;
result.userSettings = fs.getcwd() ~ result.userSettings;
result.userPackages = result.userSettings;
}
result.cache = result.userPackages ~ "cache";
Expand Down

0 comments on commit d840503

Please sign in to comment.