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

Safely handle no build tools.json config file #261

Merged
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
13 changes: 10 additions & 3 deletions src/Mobile.BuildTools.Reference/Utils/ConfigHelper.shared.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
Expand All @@ -18,7 +19,7 @@ public static string GetConfigurationPath(string searchDirectory, string slnDir
if (string.IsNullOrEmpty(searchDirectory)) return null;

var rootPath = Path.GetPathRoot(searchDirectory);
var configPath = new FileInfo(Path.Combine(searchDirectory, "buildtools.json"));
var configPath = new FileInfo(Path.Combine(searchDirectory, Constants.BuildToolsConfigFileName));
if (configPath.Exists)
{
return searchDirectory;
Expand Down Expand Up @@ -52,9 +53,15 @@ public static bool Exists(string path, out string filePath)
public static BuildToolsConfig GetConfig(string path)
{
var filePath = GetConfigurationPath(path);
if(File.GetAttributes(filePath).HasFlag(FileAttributes.Directory))
var configurationDirectoryPath = filePath;
if (File.GetAttributes(filePath).HasFlag(FileAttributes.Directory))
{
filePath = Path.Combine(filePath, "buildtools.json");
filePath = Path.Combine(filePath, Constants.BuildToolsConfigFileName);
}

if (!Exists(configurationDirectoryPath))
{
SaveDefaultConfig(configurationDirectoryPath);
}

var json = string.Empty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,92 +1,95 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.IO;
using Mobile.BuildTools.Models;
using Mobile.BuildTools.Utils;
using Xunit;
using Xunit.Abstractions;

namespace Mobile.BuildTools.Tests.Fixtures.Utils
{
public sealed class BuildToolsConfigFixture : IDisposable
public sealed class BuildToolsConfigFixture : FixtureBase
{
private static readonly string MockEmptyConfigPath = Path.Combine(Environment.CurrentDirectory, nameof(BuildToolsConfigFixture));

public BuildToolsConfigFixture()
{
Directory.CreateDirectory(MockEmptyConfigPath);
ConfigHelper.SaveDefaultConfig(MockEmptyConfigPath);
private static readonly string MockEmptyConfigPath = Path.Combine(Environment.CurrentDirectory, nameof(BuildToolsConfigFixture));

public BuildToolsConfigFixture(ITestOutputHelper testOutputHelper)
: base(MockEmptyConfigPath, testOutputHelper)
{
}

[Fact]
public void AppConfigIsNotNull()
{
var config = ConfigHelper.GetConfig(MockEmptyConfigPath);
var config = CreateConfig();

Assert.NotNull(config.AppConfig);
}

[Fact]
public void ArtifactCopyIsNotNull()
{
var config = ConfigHelper.GetConfig(MockEmptyConfigPath);
var config = CreateConfig();

Assert.NotNull(config.ArtifactCopy);
}

[Fact]
public void AutomaticVersioningIsNotNull()
{
var config = ConfigHelper.GetConfig(MockEmptyConfigPath);
var config = CreateConfig();

Assert.NotNull(config.AutomaticVersioning);
}

[Fact]
public void CssIsNotNull()
{
var config = ConfigHelper.GetConfig(MockEmptyConfigPath);
var config = CreateConfig();

Assert.NotNull(config.Css);
}

}
[Fact]
public void ImagesIsNotNull()
{
var config = ConfigHelper.GetConfig(MockEmptyConfigPath);
var config = CreateConfig();

Assert.NotNull(config.Images);
}

[Fact]
public void ManifestsIsNotNull()
{
var config = ConfigHelper.GetConfig(MockEmptyConfigPath);
var config = CreateConfig();

Assert.NotNull(config.Manifests);
}

[Fact]
public void ProjectSecretsIsNotNull()
{
var config = ConfigHelper.GetConfig(MockEmptyConfigPath);
var config = CreateConfig();

Assert.NotNull(config.ProjectSecrets);
}

[Fact]
public void ReleaseNotesIsNotNull()
{
var config = ConfigHelper.GetConfig(MockEmptyConfigPath);
var config = CreateConfig();

Assert.NotNull(config.ReleaseNotes);
}

public void Dispose()
{
if(File.Exists(MockEmptyConfigPath))
{
File.Delete(MockEmptyConfigPath);
}
}

private BuildToolsConfig CreateConfig()
{
var stackTrace = new StackTrace();
var configuration = GetConfiguration(stackTrace.GetFrame(1).GetMethod().Name);
CreateDummySolutionFile(configuration.IntermediateOutputPath);

return ConfigHelper.GetConfig(configuration.IntermediateOutputPath);
}

private static void CreateDummySolutionFile(string directory) => File.Create(Path.Combine(directory, "test.sln"));
}
}