Skip to content

Commit

Permalink
Merge pull request #2384 from fsprojects/fix_bootstrapper_finding_dep…
Browse files Browse the repository at this point in the history
…endencies

make the bootstrapper search harder for the paket.dependencies file
  • Loading branch information
matthid authored Jun 2, 2017
2 parents 55d17c0 + f108420 commit 96d8f11
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 28 deletions.
26 changes: 9 additions & 17 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,13 @@ nuget:
account_feed: false
project_feed: true

environment:
# these variables are common to all jobs
#common_var1: value1
#environment:
# # these variables are common to all jobs
# #common_var1: value1
#
# matrix:
# # first group
# - SkipIntegrationTests: true
# # second group
# - SkipNuGet: true

matrix:
# first group
- SkipIntegrationTests: true
# second group
- SkipNuGet: true

#deploy:
# provider: NuGet
# server: https://ci.appveyor.com/nuget/paket # remove to push to NuGet.org
# #api_key:
# # secure: m49OJ7+Jdt9an3jPcTukHA==
# #skip_symbols: false
# #symbol_server: # remove to push symbols to SymbolSource.org
# artifact: /temp/.*\.nupkg/
9 changes: 7 additions & 2 deletions src/Paket.Bootstrapper/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static BootstrapperOptions ParseArgumentsAndConfigurations(IEnumerable<st
if (transparentMagicMode)
{
// Transparent magic mode mean that we're renamed 'paket.exe' and --run wasn't passed

// Virtually add a '-s'
options.Verbosity -= 1;

Expand Down Expand Up @@ -105,6 +105,11 @@ public static BootstrapperOptions ParseArgumentsAndConfigurations(IEnumerable<st

options.UnprocessedCommandArgs = commandArgs;

if ("true" == Environment.GetEnvironmentVariable("PAKET_BOOTSTRAPPER_TRACE"))
{
options.Verbosity = Verbosity.Trace;
}

return options;
}

Expand Down Expand Up @@ -154,7 +159,7 @@ private static void FillRunOptionsFromArguments(BootstrapperOptions options, Lis
}
}

private static void FillNonRunOptionsFromArguments(BootstrapperOptions options, List<string> commandArgs)
internal static void FillNonRunOptionsFromArguments(BootstrapperOptions options, List<string> commandArgs)
{
if (commandArgs.Contains(CommandArgs.PreferNuget))
{
Expand Down
1 change: 1 addition & 0 deletions src/Paket.Bootstrapper/HelperProxies/FileSystemProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Paket.Bootstrapper.HelperProxies
{
class FileSystemProxy : IFileSystemProxy
{
public string GetCurrentDirectory() { return Directory.GetCurrentDirectory(); }
public bool FileExists(string filename) { return File.Exists(filename); }
public void CopyFile(string fileFrom, string fileTo, bool overwrite) { File.Copy(fileFrom, fileTo, overwrite); }
public void DeleteFile(string filename) { File.Delete(filename); }
Expand Down
1 change: 1 addition & 0 deletions src/Paket.Bootstrapper/HelperProxies/IFileSystemProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Paket.Bootstrapper.HelperProxies
{
public interface IFileSystemProxy
{
string GetCurrentDirectory();
bool FileExists(string filename);
void CopyFile(string fileFrom, string fileTo, bool overwrite = false);
void DeleteFile(string filename);
Expand Down
1 change: 1 addition & 0 deletions src/Paket.Bootstrapper/Paket.Bootstrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="BootstrapperOptions.cs" />
<Compile Include="DownloadStrategies\IDownloadStrategy.cs" />
<Compile Include="DownloadStrategies\IHaveEffectiveStrategy.cs" />
<Compile Include="Properties\InternalsVisibleTo.cs" />
<Compile Include="PaketDependencies.cs" />
<Compile Include="PaketRunner.cs" />
<Compile Include="Verbosity.cs" />
Expand Down
40 changes: 32 additions & 8 deletions src/Paket.Bootstrapper/PaketDependencies.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using System;
using Paket.Bootstrapper.HelperProxies;
using System;
using System.IO;
using System.Text.RegularExpressions;

namespace Paket.Bootstrapper
{
static class PaketDependencies
internal static class PaketDependencies
{
private static readonly Regex bootstrapperArgsLine =
new Regex("^\\s*version\\s+(?<args>.*)$", RegexOptions.IgnoreCase | RegexOptions.Compiled);

const string DEPENDENCY_FILE = "paket.dependencies";
internal const string DEPENDENCY_FILE = "paket.dependencies";

public static string GetBootstrapperArgs(TextReader reader)
{
Expand All @@ -26,27 +27,50 @@ public static string GetBootstrapperArgs(TextReader reader)
return null;
}

public static string GetBootstrapperArgsForFolder(string folder)
public static string LocateDependenciesFile(IFileSystemProxy proxy, DirectoryInfo folder)
{
var path = Path.Combine(folder.FullName, DEPENDENCY_FILE);
if (proxy.FileExists(path))
{
return path;
}
else
{
if (folder.Parent != null)
{
return LocateDependenciesFile(proxy, folder.Parent);
}
else
{
return null;
}
}
}

public static string GetBootstrapperArgsForFolder(IFileSystemProxy proxy)
{
try
{
var path = Path.Combine(folder, DEPENDENCY_FILE);
if (!File.Exists(path))
var folder = proxy.GetCurrentDirectory();
var path = LocateDependenciesFile(proxy, new DirectoryInfo(folder));
if (path == null)
{
ConsoleImpl.WriteTrace("Dependencies file was not found.");
return null;
}

using (var fileStream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var fileStream = proxy.OpenRead(path))
{
using (var reader = new StreamReader(fileStream))
{
return GetBootstrapperArgs(reader);
}
}
}
catch (Exception)
catch (Exception e)
{
// ¯\_(ツ)_/¯
ConsoleImpl.WriteTrace("Error while retrieving arguments from paket.dependencies file: {0}", e);
return null;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Paket.Bootstrapper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ static void Main(string[] args)
Console.CancelKeyPress += CancelKeyPressed;

var fileProxy = new FileSystemProxy();
var optionsBeforeDependenciesFile = ArgumentParser.ParseArgumentsAndConfigurations(args, ConfigurationManager.AppSettings,
Environment.GetEnvironmentVariables(), fileProxy, Enumerable.Empty<string>());
ConsoleImpl.Verbosity = optionsBeforeDependenciesFile.Verbosity;

var argumentsFromDependenciesFile =
WindowsProcessArguments.Parse(
PaketDependencies.GetBootstrapperArgsForFolder(Environment.CurrentDirectory));
PaketDependencies.GetBootstrapperArgsForFolder(fileProxy));
var options = ArgumentParser.ParseArgumentsAndConfigurations(args, ConfigurationManager.AppSettings,
Environment.GetEnvironmentVariables(), fileProxy, argumentsFromDependenciesFile);
if (options.ShowHelp)
Expand Down
4 changes: 4 additions & 0 deletions src/Paket.Bootstrapper/Properties/InternalsVisibleTo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Paket.Bootstrapper.Tests")]
34 changes: 34 additions & 0 deletions tests/Paket.Bootstrapper.Tests/ArgumentParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Linq;
using NUnit.Framework;
using Paket.Bootstrapper.HelperProxies;
using Moq;
using System.Text;

namespace Paket.Bootstrapper.Tests
{
Expand Down Expand Up @@ -113,6 +115,11 @@ public Stream OpenRead(string filename)
{
return Stream.Null;
}

public string GetCurrentDirectory()
{
return Directory.GetCurrentDirectory();
}
}

private static readonly IFileSystemProxy NormalModeFileSystemSystem = new DummyFileSystemProxy(Path.Combine(rootDir, "repo", ".paket", "paket.bootstrapper.exe"));
Expand Down Expand Up @@ -641,5 +648,32 @@ public void Magic_MaxFileAgeInMinutes()
Assert.That(result.DownloadArguments.MaxFileAgeInMinutes, Is.EqualTo(4242));
Assert.That(result.UnprocessedCommandArgs, Is.Empty);
}


[Test]
public void Dependencies_FindDependenciesFile()
{
//arrange
var fs = new Mock<IFileSystemProxy>();
var cwd = Directory.GetCurrentDirectory();

var subDir = Path.Combine(cwd, "testing", "subdir");
var depsFile = Path.Combine(cwd, PaketDependencies.DEPENDENCY_FILE);
var depsFileContent = "version 5.0.0-beta008";
var depsFileStream = new MemoryStream(Encoding.UTF8.GetBytes(depsFileContent));
fs.Setup(f => f.GetCurrentDirectory()).Returns(subDir);
fs.Setup(f => f.FileExists(It.IsAny<string>())).Returns(false);
fs.Setup(f => f.FileExists(depsFile)).Returns(true);
fs.Setup(f => f.OpenRead(depsFile)).Returns(depsFileStream);

//act
var opts = new BootstrapperOptions();
var argstring = PaketDependencies.GetBootstrapperArgsForFolder(fs.Object);
var args = WindowsProcessArguments.Parse(argstring);
ArgumentParser.FillNonRunOptionsFromArguments(opts, args);

//assert
Assert.That(opts.DownloadArguments.LatestVersion, Is.EqualTo("5.0.0-beta008"));
}
}
}

0 comments on commit 96d8f11

Please sign in to comment.