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, see <https://www.gnu.org/licenses/>.
18 */
19
24
26#include <qa_utils/file_utils.h>
27
28// Code under test
29#include <filename_resolver.h>
30#include <search_stack.h>
31
32#include <wx/filename.h>
33
34#include <filesystem>
35#include <fstream>
36
37namespace fs = std::filesystem;
38
39BOOST_AUTO_TEST_SUITE( FilenameResolver )
40
41BOOST_AUTO_TEST_CASE( ResolveAbsoluteAndWorkingPath )
42{
43 KI_TEST::SCOPED_TEMP_DIR tempDir( "fnres_test" );
44 fs::path work = tempDir.CreateChildDir( "work" );
45
46 fs::path file = work / "model.txt";
47 std::ofstream( file.string() ) << "dummy";
48
50
51 wxString absFile = wxString::FromUTF8( file.string() );
52 wxString resolved = resolver.ResolvePath( absFile, wxEmptyString, {} );
53 wxFileName fn( absFile );
54 fn.Normalize( wxPATH_NORM_ABSOLUTE | wxPATH_NORM_DOTS );
55 BOOST_CHECK_EQUAL( resolved, fn.GetFullPath() );
56
57 wxString working = wxString::FromUTF8( work.string() );
58 wxString relResolved = resolver.ResolvePath( wxS( "model.txt" ), working, {} );
59 BOOST_CHECK_EQUAL( relResolved, fn.GetFullPath() );
60}
61
62BOOST_AUTO_TEST_CASE( ResolveAliasAndErrors )
63{
64 KI_TEST::SCOPED_TEMP_DIR tempDir( "fnres_alias" );
65
66 fs::path file = tempDir.Path() / "a.txt";
67 std::ofstream( file.string() ) << "dummy";
68
70
71 SEARCH_PATH sp;
72 sp.m_Alias = wxS( "ALIAS" );
73 sp.m_Pathvar = tempDir.PathStr();
74 std::vector<SEARCH_PATH> paths = { sp };
75 resolver.UpdatePathList( paths );
76
77 // canonical ALIAS:path format (no leading colon)
78 wxString resolved = resolver.ResolvePath( wxS( "ALIAS:a.txt" ), wxEmptyString, {} );
79 BOOST_CHECK_EQUAL( resolved, wxString::FromUTF8( file.string() ) );
80
81 // legacy :ALIAS:path format (leading colon)
82 wxString resolvedLeadingColon = resolver.ResolvePath( wxS( ":ALIAS:a.txt" ), wxEmptyString, {} );
83 BOOST_CHECK_EQUAL( resolvedLeadingColon, wxString::FromUTF8( file.string() ) );
84
85 // unknown alias returns empty
86 wxString missing = resolver.ResolvePath( wxS( "MISSING:a.txt" ), wxEmptyString, {} );
87 BOOST_CHECK( missing.IsEmpty() );
88}
89
90// Verify that ShortenPath outputs ${ALIAS}/path and that the result round-trips through ResolvePath.
91BOOST_AUTO_TEST_CASE( ShortenPathRoundTrip )
92{
93 KI_TEST::SCOPED_TEMP_DIR tempDir( "fnres_shorten" );
94
95 fs::path file = tempDir.Path() / "sub" / "model.wrl";
96 fs::create_directories( file.parent_path() );
97 std::ofstream( file.string() ) << "dummy";
98
100
101 SEARCH_PATH sp;
102 sp.m_Alias = wxS( "MYLIB" );
103 sp.m_Pathvar = tempDir.PathStr();
104 resolver.UpdatePathList( { sp } );
105
106 wxString full = wxString::FromUTF8( file.string() );
107 wxString shortened = resolver.ShortenPath( full );
108
109 // ShortenPath produces ${ALIAS}/path for user-defined aliases
110 BOOST_CHECK( shortened.StartsWith( wxS( "${MYLIB}/" ) ) );
111
112 // The shortened path must resolve back to the original file
113 wxString resolvedBack = resolver.ResolvePath( shortened, wxEmptyString, {} );
114 wxFileName fn( full );
115 fn.Normalize( wxPATH_NORM_ABSOLUTE | wxPATH_NORM_DOTS );
116 BOOST_CHECK_EQUAL( resolvedBack, fn.GetFullPath() );
117}
118
119// Backward compatibility: files saved by older KiCad versions with :ALIAS:path format
120// for user-defined aliases must still resolve correctly.
121BOOST_AUTO_TEST_CASE( ResolveBackCompatColonAlias )
122{
123 KI_TEST::SCOPED_TEMP_DIR tempDir( "fnres_colon_alias" );
124
125 fs::path file = tempDir.Path() / "model.wrl";
126 std::ofstream( file.string() ) << "dummy";
127
129
130 SEARCH_PATH sp;
131 sp.m_Alias = wxS( "MYLIB" );
132 sp.m_Pathvar = tempDir.PathStr();
133 resolver.UpdatePathList( { sp } );
134
135 // :MYLIB:model.wrl is the legacy format produced by KiCad before e7d6c84aef
136 wxString resolved = resolver.ResolvePath( wxS( ":MYLIB:model.wrl" ), wxEmptyString, {} );
137 wxFileName fn( wxString::FromUTF8( file.string() ) );
138 fn.Normalize( wxPATH_NORM_ABSOLUTE | wxPATH_NORM_DOTS );
139 BOOST_CHECK_EQUAL( resolved, fn.GetFullPath() );
140}
141
143
144BOOST_AUTO_TEST_SUITE( SearchStack )
145
146BOOST_AUTO_TEST_CASE( RelativePathResolution )
147{
148 KI_TEST::SCOPED_TEMP_DIR tempDir( "ss_root" );
149 fs::path sub = tempDir.CreateChildDir( "sub" );
150
151 fs::path file = sub / "f.txt";
152 std::ofstream( file.string() ) << "dummy";
153
154 SEARCH_STACK stack;
155 stack.AddPaths( tempDir.PathStr() );
156
157 wxString rel = stack.FilenameWithRelativePathInSearchList( wxString::FromUTF8( file.string() ),
158 tempDir.PathStr() );
159 BOOST_CHECK_EQUAL( rel, wxString::FromUTF8( ( fs::path( "sub" ) / "f.txt" ).string() ) );
160
161 KI_TEST::SCOPED_TEMP_DIR outsideDir( "ss_outside" );
162 fs::path outside = outsideDir.Path() / "outside.txt";
163 std::ofstream( outside.string() ) << "dummy";
164
165 wxString full = stack.FilenameWithRelativePathInSearchList( wxString::FromUTF8( outside.string() ),
166 tempDir.PathStr() );
167 BOOST_CHECK_EQUAL( full, wxString::FromUTF8( outside.string() ) );
168}
169
Provide an extensible class to resolve 3D model paths.
wxString PathStr() const
Get the path to the temporary directory as a wxString.
Definition file_utils.h:62
const std::filesystem::path & Path() const
Get the path to the temporary directory as a std::filesystem::path.
Definition file_utils.h:59
std::filesystem::path CreateChildDir(const wxString &aName) const
Create and return the path to a direct child directory.
Look for files in a number of paths.
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
wxString m_Pathvar
Base path as stored in the configuration file.
wxString m_Alias
Alias to the base path.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(ResolveAbsoluteAndWorkingPath)
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")