Skip to content

Commit

Permalink
Implemented searchpath attribute for PropertyAction. #135
Browse files Browse the repository at this point in the history
  • Loading branch information
end2endzone committed Jan 13, 2024
1 parent dadeef7 commit cf81a53
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/core/ActionProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ namespace shellanything
action->SetRegistryKey(tmp_str);
}

//parse searchpath
tmp_str = "";
if (ObjectFactory::ParseAttribute(element, "searchpath", true, true, tmp_str, error))
{
action->SetSearchPath(tmp_str);
}

//done parsing
return action;
}
Expand Down Expand Up @@ -147,6 +154,21 @@ namespace shellanything
std::string file = pmgr.Expand(mFile);
std::string filesize = pmgr.Expand(mFileSize);
std::string regisrykey = pmgr.Expand(mRegistryKey);
std::string searchpath = pmgr.Expand(mSearchPath);

// If searchpath is specified, it has priority over value. This is required to allow setting a property to an empty value (a.k.a. value="").
if (!searchpath.empty())
{
// Search for a file in PATH environment variable.
std::string abs_path = ra::filesystem::FindFileFromPathsUtf8(searchpath);

// If found
if (!abs_path.empty())
{
// Store the result in 'value' as if user set this specific value (to use the same process as a property that sets a value).
value = abs_path;
}
}

// If exprtk is specified, it has priority over value. This is required to allow setting a property to an empty value (a.k.a. value="").
if (!exprtk.empty())
Expand Down Expand Up @@ -326,4 +348,14 @@ namespace shellanything
mRegistryKey = value;
}

const std::string& ActionProperty::GetSearchPath() const
{
return mSearchPath;
}

void ActionProperty::SetSearchPath(const std::string& value)
{
mSearchPath = value;
}

} //namespace shellanything
11 changes: 11 additions & 0 deletions src/core/ActionProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,24 @@ namespace shellanything
/// </summary>
void SetRegistryKey(const std::string& value);

/// <summary>
/// Getter for the 'searchpath' parameter.
/// </summary>
const std::string& GetSearchPath() const;

/// <summary>
/// Setter for the 'searchpath' parameter.
/// </summary>
void SetSearchPath(const std::string& value);

private:
std::string mName;
std::string mValue;
std::string mExprtk;
std::string mFile;
std::string mFileSize;
std::string mRegistryKey;
std::string mSearchPath;
};


Expand Down
1 change: 1 addition & 0 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(CONFIGURATION_TEST_FILES ""
${CMAKE_CURRENT_SOURCE_DIR}/test_files/TestActionProperty.testCopyFile.xml
${CMAKE_CURRENT_SOURCE_DIR}/test_files/TestActionProperty.testLiveProperties.xml
${CMAKE_CURRENT_SOURCE_DIR}/test_files/TestActionProperty.testRegistryKey.xml
${CMAKE_CURRENT_SOURCE_DIR}/test_files/TestActionProperty.testSearchPath.xml
${CMAKE_CURRENT_SOURCE_DIR}/test_files/TestConfigManager.testAssignCommandId.1.xml
${CMAKE_CURRENT_SOURCE_DIR}/test_files/TestConfigManager.testAssignCommandId.2.xml
${CMAKE_CURRENT_SOURCE_DIR}/test_files/TestConfigManager.testAssignCommandIdsInvalid.xml
Expand Down
72 changes: 72 additions & 0 deletions src/tests/TestActionProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include "rapidassist/strings.h"
#include "rapidassist/filesystem.h"
#include "rapidassist/filesystem_utf8.h"
#include "rapidassist/testing.h"
#include "rapidassist/random.h"
#include "rapidassist/undef_windows_macros.h"
Expand Down Expand Up @@ -703,6 +704,77 @@ namespace shellanything
//Cleanup
ASSERT_TRUE(workspace.Cleanup()) << "Failed deleting workspace directory '" << workspace.GetBaseDirectory() << "'.";
}
//--------------------------------------------------------------------------------------------------
TEST_F(TestActionProperty, testSearchPath)
{
ConfigManager& cmgr = ConfigManager::GetInstance();
PropertyManager& pmgr = PropertyManager::GetInstance();

//Creating a temporary workspace for the test execution.
Workspace workspace;
ASSERT_FALSE(workspace.GetBaseDirectory().empty());
ASSERT_TRUE(workspace.IsEmpty());

//Load the test Configuration File that matches this test name.
QuickLoader loader;
loader.SetWorkspace(&workspace);
ASSERT_TRUE(loader.DeleteConfigurationFilesInWorkspace());
ASSERT_TRUE(loader.LoadCurrentTestConfigurationFile());

//Get all menus.
ConfigFile::ConfigFilePtrList configs = cmgr.GetConfigFiles();
ASSERT_EQ(1, configs.size());

//ASSERT a single menu is available
Menu::MenuPtrList menus = cmgr.GetConfigFiles()[0]->GetMenus();
ASSERT_GT(menus.size(), 1);

//Clear properties
static const char* properties[] = {
"test1",
"test2",
"test3",
"test4",
};
static const size_t properties_count = sizeof(properties) / sizeof(properties[0]);
for (size_t i = 0; i < properties_count; i++)
{
pmgr.ClearProperty(properties[i]);
}

//Create a valid context
SelectionContext c;
StringList elements;
elements.push_back("C:\\Windows");
c.SetElements(elements);
c.RegisterProperties();

// Execute
for (size_t i = 0; i < menus.size(); i++)
{
Menu* menu = menus[i];
bool executed = ActionManager::Execute(menu, c);
ASSERT_TRUE(executed) << "Failed to execute actions of menu '" << menu->GetName() << "'.";
}

//ASSERT the properties were set
for (size_t i = 0; i < properties_count; i++)
{
const char* property_name = properties[i];
ASSERT_TRUE(pmgr.HasProperty(property_name)) << "Property not found: '" << property_name << "'.";
}

//ASSERT the properties matches a real file
for (size_t i = 0; i < properties_count; i++)
{
const char* property_name = properties[i];
const std::string& file_path = pmgr.GetProperty(property_name);
ASSERT_TRUE(ra::filesystem::FileExistsUtf8(file_path.c_str())) << "File does not exists: '" << file_path << "'.";
}

//Cleanup
ASSERT_TRUE(workspace.Cleanup()) << "Failed deleting workspace directory '" << workspace.GetBaseDirectory() << "'.";
}

} //namespace test
} //namespace shellanything
8 changes: 7 additions & 1 deletion src/tests/TestObjectFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ namespace shellanything

//ASSERT a multiple menus are available
Menu::MenuPtrList menus = cmgr.GetConfigFiles()[0]->GetMenus();
ASSERT_EQ(6, menus.size());
ASSERT_EQ(7, menus.size());

//Assert all menus have a property element as the first action
ActionProperty* property00 = GetFirstActionProperty(menus[00]);
Expand All @@ -705,13 +705,15 @@ namespace shellanything
ActionProperty* property03 = GetFirstActionProperty(menus[03]);
ActionProperty* property04 = GetFirstActionProperty(menus[04]);
ActionProperty* property05 = GetFirstActionProperty(menus[05]);
ActionProperty* property06 = GetFirstActionProperty(menus[06]);

ASSERT_TRUE(property00 != NULL);
ASSERT_TRUE(property01 != NULL);
ASSERT_TRUE(property02 != NULL);
ASSERT_TRUE(property03 != NULL);
ASSERT_TRUE(property04 != NULL);
ASSERT_TRUE(property05 != NULL);
ASSERT_TRUE(property06 != NULL);

//Assert menu #0 have a name and a value parsed
static const std::string EMPTY_STRING;
Expand Down Expand Up @@ -742,6 +744,10 @@ namespace shellanything
std::string property05_registrykey = property05->GetRegistryKey();
ASSERT_EQ(std::string("HKEY_LOCAL_MACHINE\\SOFTWARE\\7-Zip\\Path"), property05_registrykey);

//Assert menu #5 have a searchpath attribute parsed
std::string property06_searchpath = property06->GetSearchPath();
ASSERT_EQ(std::string("foobar.exe"), property06_searchpath);

//Cleanup
ASSERT_TRUE(workspace.Cleanup()) << "Failed deleting workspace directory '" << workspace.GetBaseDirectory() << "'.";
}
Expand Down
34 changes: 34 additions & 0 deletions src/tests/test_files/TestActionProperty.testSearchPath.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<shell>

<menu name="menu1">
<actions>
<!-- Find `explorer.exe` in Windows directory -->
<property name="test1" searchpath="explorer.exe" />
</actions>
</menu>

<menu name="menu2">
<actions>
<!-- Find `python.exe` interpreter in an unknown directory -->
<property name="test2" searchpath="python.exe" />
</actions>
</menu>

<menu name="menu3">
<actions>
<!-- Find `cmd.exe` in Windows System32 directory -->
<property name="test3" searchpath="cmd.exe" />
</actions>
</menu>

<menu name="menu4">
<actions>
<!-- Find `git.exe` command -->
<property name="test4" searchpath="git.exe" />
</actions>
</menu>

</shell>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
<property name="foo" registrykey="HKEY_LOCAL_MACHINE\SOFTWARE\7-Zip\Path" />
</actions>
</menu>

<menu name="menu06">
<actions>
<!--basic searchpath usage-->
<property name="foo" searchpath="foobar.exe" />
</actions>
</menu>

</shell>
</root>

0 comments on commit cf81a53

Please sign in to comment.