Skip to content

Commit

Permalink
Improve error messages expanding single-file (#58557)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitek-karas committed Sep 13, 2021
1 parent dc43645 commit 1517985
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,58 @@ private void Bundle_extraction_can_recover_missing_files()
extractDir.Should().HaveFiles(extractedFiles);
}

[Fact]
private void Bundle_extraction_to_nonexisting_default()
{
string nonExistentPath = Path.Combine(
sharedTestState.DefaultBundledAppFixture.TestProject.OutputDirectory,
"nonexistent");

string defaultExpansionEnvVariable = OperatingSystem.IsWindows() ? "TMP" : "HOME";
string expectedErrorMessagePart = OperatingSystem.IsWindows() ?
$"Failed to determine default extraction location. Check if 'TMP'" :
$"Default extraction directory [{nonExistentPath}] either doesn't exist or is not accessible for read/write.";

Command.Create(sharedTestState.DefaultBundledAppExecutablePath)
.CaptureStdErr()
.CaptureStdOut()
.EnvironmentVariable(defaultExpansionEnvVariable, nonExistentPath)
.Execute().Should().Fail()
.And.HaveStdErrContaining(expectedErrorMessagePart);
}

[Fact]
[SkipOnPlatform(TestPlatforms.Windows, "On Windows the default extraction path is determined by calling GetTempPath which looks at multiple places and can't really be undefined.")]
private void Bundle_extraction_default_undefined()
{
Command.Create(sharedTestState.DefaultBundledAppExecutablePath)
.CaptureStdErr()
.CaptureStdOut()
.EnvironmentVariable("HOME", null)
.Execute().Should().Fail()
.And.HaveStdErrContaining("Failed to determine default extraction location. Environment variable '$HOME' is not defined.");
}

public class SharedTestState : SharedTestStateBase, IDisposable
{
public TestProjectFixture TestFixture { get; set; }
public TestProjectFixture TestFixture { get; }

public TestProjectFixture DefaultBundledAppFixture { get; }
public string DefaultBundledAppExecutablePath { get; }
public Bundler DefaultBundledAppBundler { get; }

public SharedTestState()
{
TestFixture = PreparePublishedSelfContainedTestProject("StandaloneApp");

DefaultBundledAppFixture = TestFixture.Copy();
DefaultBundledAppBundler = BundleSelfContainedApp(DefaultBundledAppFixture, out var singleFile, BundleOptions.BundleNativeBinaries);
DefaultBundledAppExecutablePath = singleFile;
}

public void Dispose()
{
DefaultBundledAppFixture.Dispose();
TestFixture.Dispose();
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/native/corehost/hostmisc/pal.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,18 @@ bool get_extraction_base_parent_directory(pal::string_t& directory)
// check for the POSIX standard environment variable
if (pal::getenv(_X("HOME"), &directory))
{
return is_read_write_able_directory(directory);
if (is_read_write_able_directory(directory))
{
return true;
}
else
{
trace::error(_X("Default extraction directory [%s] either doesn't exist or is not accessible for read/write."), directory.c_str());
}
}
else
{
trace::error(_X("Failed to determine default extraction location. Environment variable '$HOME' is not defined."));
}

return false;
Expand All @@ -367,6 +378,7 @@ bool pal::get_default_bundle_extraction_base_dir(pal::string_t& extraction_dir)
}
else if (errno != EEXIST)
{
trace::error(_X("Failed to create default extraction directory [%s]. %s"), extraction_dir.c_str(), pal::strerror(errno));
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions src/native/corehost/hostmisc/pal.windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ bool pal::get_default_bundle_extraction_base_dir(pal::string_t& extraction_dir)
{
if (!get_extraction_base_parent_directory(extraction_dir))
{
trace::error(_X("Failed to determine default extraction location. Check if 'TMP' or 'TEMP' points to existing path."));
return false;
}

Expand All @@ -588,6 +589,7 @@ bool pal::get_default_bundle_extraction_base_dir(pal::string_t& extraction_dir)
if (CreateDirectoryW(extraction_dir.c_str(), NULL) == 0 &&
GetLastError() != ERROR_ALREADY_EXISTS)
{
trace::error(_X("Failed to create default extraction directory [%s]. %s, error code: %d"), extraction_dir.c_str(), pal::strerror(errno), GetLastError());
return false;
}

Expand Down

0 comments on commit 1517985

Please sign in to comment.