Skip to content

Commit

Permalink
Dyn 3598 (#11800)
Browse files Browse the repository at this point in the history
* small adjustment

* align titles

* add names to buttons for ui automation tests

* missing button name

* disable program data in ui
add some todos to try to eliminate properties that are redudant
some name changes I hope help

* add some todos for reducing use of package loader defaultpackagesdir
spelling
use pathmanager instead of package loder at package man extension startup
test that both defautldirectories are the same

* create failing test...

* remove todo

* fix package tests - this check is already done in tech debt test

Co-authored-by: michael kirschner <[email protected]>
  • Loading branch information
mjkkirschner and mjkkirschner authored Jun 30, 2021
1 parent 3c700d9 commit ce29111
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/DynamoCoreWpf/ViewModels/Menu/PreferencesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ public string SelectedPackagePathForInstall
if (preferenceSettings.SelectedPackagePathForInstall != value)
{
preferenceSettings.SelectedPackagePathForInstall = value;
//TODO remove along with PackageLoader.DefaultPackagesDirectory and PackageLoader.DefaultPackagesDirectoryIndex
dynamoViewModel.PackageManagerClientViewModel.PackageManagerExtension.PackageLoader.SetPackagesDownloadDirectory(
value, dynamoViewModel.Model.PathManager.UserDataDirectory);
RaisePropertyChanged(nameof(SelectedPackagePathForInstall));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Dynamo.Models;
using System.Windows.Data;
using System.Globalization;
using System.Linq;

namespace Dynamo.ViewModels
{
Expand Down Expand Up @@ -130,7 +131,9 @@ private void RaiseCanExecuteChanged()

private bool CanDelete(int param)
{
if (RootLocations.IndexOf(Resources.PackagePathViewModel_Standard_Library) == param)
var programDataPackagePathIndex = GetIndexOfProgramDataPackagePath();
if (RootLocations.IndexOf(Resources.PackagePathViewModel_Standard_Library) == param ||
programDataPackagePathIndex == param)
{
return false;
}
Expand All @@ -150,7 +153,19 @@ private bool CanMoveDown(int param)

private bool CanUpdate(int param)
{
return RootLocations.IndexOf(Resources.PackagePathViewModel_Standard_Library) != param;
var programDataPackagePathIndex = GetIndexOfProgramDataPackagePath();

//editing builtin packages or programData package paths is not allowed.
return RootLocations.IndexOf(Resources.PackagePathViewModel_Standard_Library) != param &&
programDataPackagePathIndex != param;
}

private int GetIndexOfProgramDataPackagePath()
{
var programDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var programDataPackagePath = RootLocations.Where(x => x.StartsWith(programDataPath)).FirstOrDefault();
var programDataPackagePathIndex = RootLocations.IndexOf(programDataPackagePath);
return programDataPackagePathIndex;
}

// The position of the selected entry must always be the first parameter.
Expand Down
10 changes: 4 additions & 6 deletions src/DynamoPackages/PackageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@ public IEnumerable<IExtension> RequestedExtensions

private readonly List<string> packagesDirectories = new List<string>();


//TODO remove.
private int defaultPackagesDirectoryIndex = -1;
//TODO this should get DefaultPackagesDirectoryFrom PathManager directly.
/// <summary>
/// Returns the default package directory where new packages will be installed
/// This is the first non standard library directory
/// The first entry is the standard library.
/// </summary>
/// <returns>Returns the path to the DefaultPackagesDirectory if found - or null if something has gone wrong.</returns>
[Obsolete("This property is redundant, please use the PathManager.DefaultPackagesDirectory property instead.")]
public string DefaultPackagesDirectory
{
get { return defaultPackagesDirectoryIndex != -1 ? packagesDirectories[defaultPackagesDirectoryIndex] : null; }
Expand Down Expand Up @@ -114,6 +116,7 @@ private static string TransformPath(string root, string userDataFolder, string e
return root;
}

//TODO remove when removing DefaultPackagesDirectory and DefaultPackagesDirectoryIndex
internal void SetPackagesDownloadDirectory(string downloadDirectory, string userDataFolder)
{
defaultPackagesDirectoryIndex = packagesDirectories.IndexOf(
Expand Down Expand Up @@ -214,11 +217,6 @@ private void InitPackageLoader(IEnumerable<string> packagesDirectories, string s
defaultPackagesDirectoryIndex = standardLibraryIndex == 1 ? 0 : safeIndex;
}

var error = PathHelper.CreateFolderIfNotExist(DefaultPackagesDirectory);

if (error != null)
Log(error);

packagesDirectoriesToVerifyCertificates.Add(stdLibDirectory);
}

Expand Down
12 changes: 6 additions & 6 deletions src/DynamoPackages/PackageManagerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public class PackageManagerClient
private readonly IPackageUploadBuilder uploadBuilder;

/// <summary>
/// The directory where all packages are to be stored for this session.
/// The directory where new packages are created during the upload process.
/// </summary>
private readonly string packagesDirectory;
private readonly string packageUploadDirectory;

/// <summary>
/// The URL of the package manager website
Expand All @@ -42,9 +42,9 @@ public string BaseUrl

#endregion

internal PackageManagerClient(IGregClient client, IPackageUploadBuilder builder, string packagesDirectory)
internal PackageManagerClient(IGregClient client, IPackageUploadBuilder builder, string packageUploadDirectory)
{
this.packagesDirectory = packagesDirectory;
this.packageUploadDirectory = packageUploadDirectory;
this.uploadBuilder = builder;
this.client = client;
}
Expand Down Expand Up @@ -184,14 +184,14 @@ internal void Publish(Package package, IEnumerable<string> files, bool isNewVers
ResponseBody ret = null;
if (isNewVersion)
{
var pkg = uploadBuilder.NewPackageVersionUpload(package, packagesDirectory, files,
var pkg = uploadBuilder.NewPackageVersionUpload(package, packageUploadDirectory, files,
packageUploadHandle);
packageUploadHandle.UploadState = PackageUploadHandle.State.Uploading;
ret = this.client.ExecuteAndDeserialize(pkg);
}
else
{
var pkg = uploadBuilder.NewPackageUpload(package, packagesDirectory, files,
var pkg = uploadBuilder.NewPackageUpload(package, packageUploadDirectory, files,
packageUploadHandle);
packageUploadHandle.UploadState = PackageUploadHandle.State.Uploading;
ret = this.client.ExecuteAndDeserialize(pkg);
Expand Down
12 changes: 6 additions & 6 deletions src/DynamoPackages/PackageManagerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,21 @@ public void Startup(StartupParams startupParams)
PackageUploadBuilder.SetEngineVersion(startupParams.DynamoVersion);
var uploadBuilder = new PackageUploadBuilder(dirBuilder, new MutatingFileCompressor());

// Align the package install directory with the package download directory -
// Align the package upload directory with the package download directory -
// either the one selected by the user or the default directory.
string packageInstallDirectory;
string packageUploadDirectory;
if (startupParams.Preferences is PreferenceSettings preferences)
{
packageInstallDirectory = string.IsNullOrEmpty(preferences.SelectedPackagePathForInstall) ?
PackageLoader.DefaultPackagesDirectory : preferences.SelectedPackagePathForInstall;
packageUploadDirectory = string.IsNullOrEmpty(preferences.SelectedPackagePathForInstall) ?
startupParams.PathManager.DefaultPackagesDirectory : preferences.SelectedPackagePathForInstall;
}
else
{
packageInstallDirectory = PackageLoader.DefaultPackagesDirectory;
packageUploadDirectory = startupParams.PathManager.DefaultPackagesDirectory;
}
PackageManagerClient = new PackageManagerClient(
new GregClient(startupParams.AuthProvider, url),
uploadBuilder, packageInstallDirectory);
uploadBuilder, packageUploadDirectory);

LoadPackages(startupParams.Preferences, startupParams.PathManager);
}
Expand Down
19 changes: 19 additions & 0 deletions test/DynamoCoreWpfTests/DynamoTestUIBase.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 Down Expand Up @@ -231,6 +232,24 @@ public NoteView NoteViewWithGuid(string guid)
return noteViewsOfType.First();
}

protected static string GetAppDataFolder()
{
var folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var dynamoVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
var appDataFolder = Path.Combine(Path.Combine(folder, "Dynamo", "Dynamo Core"),
$"{dynamoVersion.FileMajorPart}.{dynamoVersion.FileMinorPart}");

return appDataFolder;
}
protected static string GetCommonDataDirectory()
{
var folder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var dynamoVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
var commonDataFolder = Path.Combine(Path.Combine(folder, "Dynamo", "Dynamo Core"),
$"{dynamoVersion.FileMajorPart}.{dynamoVersion.FileMinorPart}");
return commonDataFolder;
}

#endregion
}
}
77 changes: 76 additions & 1 deletion test/DynamoCoreWpfTests/PackagePathTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

using System.IO;
using System.Linq;
using System.Reflection;
using Dynamo.Configuration;
using Dynamo.Core;
using Dynamo.Interfaces;
using Dynamo.Models;
using Dynamo.PackageManager;
using Dynamo.Scheduler;
using Dynamo.ViewModels;
using NUnit.Framework;
using SystemTestServices;
Expand Down Expand Up @@ -86,6 +89,37 @@ public void CannotUpdateStandardLibraryPath()
};


var vm = CreatePackagePathViewModel(setting);

Assert.AreEqual(2, vm.RootLocations.Count);
Assert.IsFalse(vm.UpdatePathCommand.CanExecute(0));
Assert.IsTrue(vm.UpdatePathCommand.CanExecute(1));
}
[Test]
public void CannotDeleteProgramDataPath()
{
var setting = new PreferenceSettings
{
CustomPackageFolders = { Path.Combine(ViewModel.Model.PathManager.CommonDataDirectory,"Packages"), @"C:\" }
};


var vm = CreatePackagePathViewModel(setting);

Assert.AreEqual(2, vm.RootLocations.Count);
Assert.IsFalse(vm.DeletePathCommand.CanExecute(0));
Assert.IsTrue(vm.DeletePathCommand.CanExecute(1));
}

[Test]
public void CannotUpdateProgramDataPath()
{
var setting = new PreferenceSettings
{
CustomPackageFolders = { Path.Combine(ViewModel.Model.PathManager.CommonDataDirectory, "Packages"), @"C:\" }
};


var vm = CreatePackagePathViewModel(setting);

Assert.AreEqual(2, vm.RootLocations.Count);
Expand Down Expand Up @@ -186,6 +220,8 @@ public void PathEnabledConverterStdLibPath()
Assert.False((bool)x.Convert(new object[] { vm, @"Z:\" }, null, null, null));
}



#endregion
#region Setup methods
private PackagePathViewModel CreatePackagePathViewModel(PreferenceSettings setting)
Expand All @@ -199,7 +235,46 @@ private PackagePathViewModel CreatePackagePathViewModel(PreferenceSettings setti
CustomNodeManager customNodeManager = Model.CustomNodeManager;
return new PackagePathViewModel(loader, loadParams, customNodeManager);
}

#endregion
}

class PackagePathTests_CustomPrefs : DynamoTestUIBase
{
/// <summary>
/// Derived test classes can override this method to provide different configurations.
/// </summary>
/// <param name="pathResolver">A path resolver to pass to the DynamoModel. </param>
protected override DynamoModel.IStartConfiguration CreateStartConfiguration(IPathResolver pathResolver)
{
return new DynamoModel.DefaultStartConfiguration()
{
PathResolver = pathResolver,
StartInTestMode = true,
GeometryFactoryPath = preloader.GeometryFactoryPath,
ProcessMode = TaskProcessMode.Synchronous,
Preferences = new PreferenceSettings()
{
//program data first
CustomPackageFolders = { Path.Combine(GetCommonDataDirectory(),PathManager.PackagesDirectoryName), @"C:\", GetAppDataFolder(), }
}
};
}

[Test]
[Category("TechDebt")]
public void IfProgramDataPathIsFirstDefaultPackagePathIsStillAppData()
{
var setting = Model.PreferenceSettings;
var loader = Model.GetPackageManagerExtension().PackageLoader;

var appDataFolder = Path.Combine(GetAppDataFolder());
Assert.AreEqual(3, ViewModel.Model.PathManager.PackagesDirectories.Count());
Assert.AreEqual(4, setting.CustomPackageFolders.Count);
Assert.AreEqual(Path.Combine(appDataFolder, PathManager.PackagesDirectoryName), ViewModel.Model.PathManager.DefaultPackagesDirectory);
Assert.AreEqual(appDataFolder, setting.SelectedPackagePathForInstall);
//TODO this is incorrect, but this property has been obsoleted and will be made correct after refactoring pathmanager and packageloader.
//this should be changed to Assert.AreEqual after refactor.
Assert.AreNotEqual(Path.Combine(appDataFolder, PathManager.PackagesDirectoryName), loader.DefaultPackagesDirectory);
}
}
}
2 changes: 0 additions & 2 deletions test/Libraries/SystemTestServices/SystemTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using System.Reflection;
using System.Threading;

using Dynamo.Configuration;
using Dynamo.Controls;
using Dynamo.Extensions;
Expand All @@ -16,7 +15,6 @@
using Dynamo.Tests;
using Dynamo.Updates;
using Dynamo.ViewModels;
using Dynamo.Wpf.Extensions;
using DynamoShapeManager;

using NUnit.Framework;
Expand Down

0 comments on commit ce29111

Please sign in to comment.