KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_filename_resolver.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
30
31// Code under test
32#include <filename_resolver.h>
33#include <search_stack.h>
34
35#include <wx/filename.h>
36
37#include <filesystem>
38#include <fstream>
39
40namespace fs = std::filesystem;
41
42BOOST_AUTO_TEST_SUITE( FilenameResolver )
43
44BOOST_AUTO_TEST_CASE( ResolveAbsoluteAndWorkingPath )
45{
46 fs::path temp = fs::temp_directory_path() / "fnres_test";
47 fs::create_directories( temp );
48
49 fs::path work = temp / "work";
50 fs::create_directories( work );
51
52 fs::path file = work / "model.txt";
53 std::ofstream( file.string() ) << "dummy";
54
56
57 wxString absFile = wxString::FromUTF8( file.string() );
58 wxString resolved = resolver.ResolvePath( absFile, wxEmptyString, {} );
59 wxFileName fn( absFile );
60 fn.Normalize( wxPATH_NORM_ABSOLUTE | wxPATH_NORM_DOTS );
61 BOOST_CHECK_EQUAL( resolved, fn.GetFullPath() );
62
63 wxString working = wxString::FromUTF8( work.string() );
64 wxString relResolved = resolver.ResolvePath( wxS( "model.txt" ), working, {} );
65 BOOST_CHECK_EQUAL( relResolved, fn.GetFullPath() );
66}
67
68BOOST_AUTO_TEST_CASE( ResolveAliasAndErrors )
69{
70 fs::path temp = fs::temp_directory_path() / "fnres_alias";
71 fs::create_directories( temp );
72
73 fs::path file = temp / "a.txt";
74 std::ofstream( file.string() ) << "dummy";
75
77
78 SEARCH_PATH sp;
79 sp.m_Alias = wxS( "ALIAS" );
80 sp.m_Pathvar = wxString::FromUTF8( temp.string() );
81 std::vector<SEARCH_PATH> paths = { sp };
82 resolver.UpdatePathList( paths );
83
84 wxString resolved = resolver.ResolvePath( wxS( "ALIAS:a.txt" ), wxEmptyString, {} );
85 BOOST_CHECK_EQUAL( resolved, wxString::FromUTF8( file.string() ) );
86
87 wxString missing = resolver.ResolvePath( wxS( "MISSING:a.txt" ), wxEmptyString, {} );
88 BOOST_CHECK( missing.IsEmpty() );
89}
90
92
93BOOST_AUTO_TEST_SUITE( SearchStack )
94
95BOOST_AUTO_TEST_CASE( RelativePathResolution )
96{
97 fs::path root = fs::temp_directory_path() / "ss_root";
98 fs::path sub = root / "sub";
99 fs::create_directories( sub );
100
101 fs::path file = sub / "f.txt";
102 std::ofstream( file.string() ) << "dummy";
103
104 SEARCH_STACK stack;
105 stack.AddPaths( wxString::FromUTF8( root.string() ) );
106
107 wxString rel = stack.FilenameWithRelativePathInSearchList( wxString::FromUTF8( file.string() ),
108 wxString::FromUTF8( root.string() ) );
109 BOOST_CHECK_EQUAL( rel, wxString::FromUTF8( ( fs::path( "sub" ) / "f.txt" ).string() ) );
110
111 fs::path outside = fs::temp_directory_path() / "outside.txt";
112 std::ofstream( outside.string() ) << "dummy";
113
114 wxString full = stack.FilenameWithRelativePathInSearchList( wxString::FromUTF8( outside.string() ),
115 wxString::FromUTF8( root.string() ) );
116 BOOST_CHECK_EQUAL( full, wxString::FromUTF8( outside.string() ) );
117}
118
Provide an extensible class to resolve 3D model paths.
wxString ResolvePath(const wxString &aFileName, const wxString &aWorkingPath, std::vector< const EMBEDDED_FILES * > aEmbeddedFilesStack)
Determine the full path of the given file name.
bool UpdatePathList(const std::vector< SEARCH_PATH > &aPathList)
Clear the current path list and substitutes the given path list and update the path configuration fil...
Look for files in a number of paths.
Definition: search_stack.h:43
void AddPaths(const wxString &aPaths, int aIndex=-1)
Insert or append path(s).
wxString FilenameWithRelativePathInSearchList(const wxString &aFullFilename, const wxString &aBaseDir)
Return the shortest possible path which can be use later to find a full path from this SEARCH_STACK.
static FILENAME_RESOLVER * resolver
Definition: export_idf.cpp:53
wxString m_Pathvar
Base path as stored in the configuration file.
wxString m_Alias
Alias to the base path.
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK_EQUAL(ret, c.m_exp_result)
BOOST_AUTO_TEST_CASE(ResolveAbsoluteAndWorkingPath)
BOOST_AUTO_TEST_SUITE_END()