Skip to content

Commit f27212c

Browse files
committed
- correct issues with long filenames as just partially fixed by N++ with notepad-plus-plus/notepad-plus-plus#11106
- added assert checks to testcases for the new methods with bool return values - removed not implemented declaration
1 parent cc4bf55 commit f27212c

3 files changed

Lines changed: 65 additions & 75 deletions

File tree

PythonScript/python_tests/tests/NotepadWrapperTestCase.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ def find_child_window(self, caption):
141141

142142
def test_setEncoding(self):
143143
notepad.new()
144-
notepad.setEncoding(BUFFERENCODING.UTF8)
144+
self.assertTrue(notepad.setEncoding(BUFFERENCODING.UTF8))
145145
encoding = notepad.getEncoding()
146146
self.assertEqual(encoding, BUFFERENCODING.UTF8)
147-
notepad.setEncoding(BUFFERENCODING.ANSI)
147+
self.assertTrue(notepad.setEncoding(BUFFERENCODING.ANSI))
148148
encoding = notepad.getEncoding()
149149
self.assertEqual(encoding, BUFFERENCODING.ANSI)
150150
notepad.close()
@@ -184,22 +184,22 @@ def test_save(self):
184184

185185
def test_saveAs(self):
186186
notepad.new()
187-
notepad.setEncoding(BUFFERENCODING.ANSI)
187+
self.assertTrue(notepad.setEncoding(BUFFERENCODING.ANSI))
188188
editor.write('Hello world - saveAs')
189189
filename = self.get_temp_filename()
190-
notepad.saveAs(filename)
190+
self.assertTrue(notepad.saveAs(filename))
191191
notepad.close()
192192
self.check_file_contents(filename, 'Hello world - saveAs')
193193

194194

195195
def test_saveAsCopy(self):
196196
notepad.new()
197-
notepad.setEncoding(BUFFERENCODING.ANSI)
197+
self.assertTrue(notepad.setEncoding(BUFFERENCODING.ANSI))
198198
editor.write('Hello world - saveAsCopy')
199199
realFilename = self.get_temp_filename()
200200
copyFilename = self.get_temp_filename()
201-
notepad.saveAs(realFilename)
202-
notepad.saveAsCopy(copyFilename)
201+
self.assertTrue(notepad.saveAs(realFilename))
202+
self.assertTrue(notepad.saveAsCopy(copyFilename))
203203
editor.appendText('-OriginalChanged')
204204
notepad.save()
205205
notepad.close()
@@ -212,7 +212,7 @@ def test_open(self):
212212
f = open(filename, "w")
213213
f.write('Test - open')
214214
f.close()
215-
notepad.open(filename)
215+
self.assertTrue(notepad.open(filename))
216216
text = editor.getText()
217217
notepad.close()
218218

@@ -261,13 +261,13 @@ def test_getFiles(self):
261261
notepad.new()
262262
editor.write('File 1')
263263
file1 = self.get_temp_filename()
264-
notepad.saveAs(file1)
264+
self.assertTrue(notepad.saveAs(file1))
265265
bufferID1 = notepad.getCurrentBufferID()
266266
index1 = notepad.getCurrentDocIndex(0)
267267
notepad.new()
268268
editor.write('File 2')
269269
file2 = self.get_temp_filename()
270-
notepad.saveAs(file2)
270+
self.assertTrue(notepad.saveAs(file2))
271271
bufferID2 = notepad.getCurrentBufferID()
272272
index2 = notepad.getCurrentDocIndex(0)
273273

@@ -297,10 +297,10 @@ def test_saveLoadSession(self):
297297
# Create and open two files
298298
file1 = self.get_temp_filename()
299299
file2 = self.get_temp_filename()
300-
notepad.open(file1)
300+
self.assertTrue(notepad.open(file1))
301301
editor.write('File 1 session')
302302
notepad.save()
303-
notepad.open(file2)
303+
self.assertTrue(notepad.open(file2))
304304
editor.write('File 2 session')
305305
notepad.save()
306306

@@ -309,19 +309,19 @@ def test_saveLoadSession(self):
309309
notepad.saveSession(sessionFile, [file1, file2])
310310

311311
# Close the files
312-
notepad.activateFile(file1)
312+
self.assertTrue(notepad.activateFile(file1))
313313
notepad.close()
314-
notepad.activateFile(file2)
314+
self.assertTrue(notepad.activateFile(file2))
315315
notepad.close()
316316

317317
# Load the session back
318318
notepad.loadSession(sessionFile)
319319

320320
# Check the files are there again
321-
notepad.activateFile(file1)
321+
self.assertTrue(notepad.activateFile(file1))
322322
file1Content = editor.getText()
323323
notepad.close()
324-
notepad.activateFile(file2)
324+
self.assertTrue(notepad.activateFile(file2))
325325
file2Content = editor.getText()
326326
notepad.close()
327327

@@ -336,7 +336,7 @@ def test_getSessionFiles(self):
336336
notepad.open(file1)
337337
editor.write('File 1 session')
338338
notepad.save()
339-
notepad.open(file2)
339+
self.assertTrue(notepad.open(file2))
340340
editor.write('File 2 session')
341341
notepad.save()
342342

@@ -345,9 +345,9 @@ def test_getSessionFiles(self):
345345
notepad.saveSession(sessionFile, [file1, file2])
346346
sessionFiles = notepad.getSessionFiles(sessionFile)
347347

348-
notepad.activateFile(file1)
348+
self.assertTrue(notepad.activateFile(file1))
349349
notepad.close()
350-
notepad.activateFile(file2)
350+
self.assertTrue(notepad.activateFile(file2))
351351
notepad.close()
352352
normalisedSessionFiles = [self.normalise_filename(f) for f in sessionFiles]
353353
self.assertEqual(normalisedSessionFiles, [self.normalise_filename(file1), self.normalise_filename(file2)])
@@ -361,16 +361,16 @@ def test_saveCurrentSession(self):
361361
notepad.open(file1)
362362
editor.write('File 1 session')
363363
notepad.save()
364-
notepad.open(file2)
364+
self.assertTrue(notepad.open(file2))
365365
editor.write('File 2 session')
366366
notepad.save()
367367

368368
sessionFile = self.get_temp_filename()
369369
notepad.saveCurrentSession(sessionFile)
370370

371-
notepad.activateFile(file1)
371+
self.assertTrue(notepad.activateFile(file1))
372372
notepad.close()
373-
notepad.activateFile(file2)
373+
self.assertTrue(notepad.activateFile(file2))
374374
notepad.close()
375375

376376
sessionFiles = notepad.getSessionFiles(sessionFile)
@@ -430,12 +430,13 @@ def store_silent_updates(hwnd, lParam):
430430
notepad.new()
431431
editor.write('Reload test')
432432
filename = self.get_temp_filename()
433-
notepad.saveAs(filename)
433+
self.assertTrue(notepad.saveAs(filename))
434434
f = open(filename, "w")
435435
f.write('Updated outside')
436436
f.close()
437437
beforeReload = editor.getText()
438-
notepad.reloadFile(filename, False)
438+
# TODO: See https://github.com/notepad-plus-plus/notepad-plus-plus/issues/12418
439+
self.assertFalse(notepad.reloadFile(filename, False))
439440
afterReload = editor.getText()
440441
notepad.close()
441442

@@ -1026,7 +1027,7 @@ def test_hideTabBar(self):
10261027
with self.assertRaises(ArgumentError):
10271028
self._invalid_parameter_passed(notepad_method, '')
10281029
hidden_tab_bar = notepad.hideTabBar()
1029-
self.assertIsNone(hidden_tab_bar)
1030+
self.assertFalse(hidden_tab_bar)
10301031
self.assertTrue(notepad.isTabBarHidden())
10311032

10321033

@@ -1334,18 +1335,15 @@ def test_saveAllFiles(self):
13341335
def test_saveFile(self):
13351336
''' '''
13361337
self.__test_invalid_parameter_passed(notepad.saveFile)
1337-
self.assertIsNone(notepad.saveFile(''))
1338+
self.assertFalse(notepad.saveFile(''))
13381339

13391340
tmpfile = self.get_temp_filename()
1340-
notepad.open(tmpfile)
1341+
self.assertTrue(notepad.open(tmpfile))
13411342
text_to_be_saved = 'example_text'
13421343
editor.addText(text_to_be_saved)
13431344
self.check_file_contents(tmpfile, '')
13441345

1345-
notepad.saveFile(tmpfile)
1346-
# N++ doesn't save with saveFile(), therefore saveAs() is called additionally
1347-
# to make this testcase working
1348-
notepad.saveAs(tmpfile)
1346+
self.assertTrue(notepad.saveFile(tmpfile))
13491347

13501348
self.check_file_contents(tmpfile, text_to_be_saved)
13511349

PythonScript/src/NotepadPlusWrapper.cpp

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ bool NotepadPlusWrapper::addCallback(boost::python::object callback, boost::pyth
186186

187187
m_notificationsEnabled = true;
188188

189-
return true;
189+
return true;
190190
}
191191
else
192192
{
@@ -295,12 +295,8 @@ boost::python::list NotepadPlusWrapper::getFiles()
295295
bufferID = callNotepad(NPPM_GETBUFFERIDFROMPOS, pos, view);
296296
if (bufferID)
297297
{
298-
#ifdef UNICODE
299298
std::shared_ptr<char> mbFilename = WcharMbcsConverter::tchar2char(fileNames[pos]);
300299
files.append(boost::python::make_tuple(const_cast<const char*>(mbFilename.get()), bufferID, pos, view));
301-
#else
302-
files.append(boost::python::make_tuple(const_cast<const char*>(fileNames[pos]), bufferID, pos, view));
303-
#endif
304300
}
305301
}
306302
}
@@ -319,12 +315,8 @@ boost::python::list NotepadPlusWrapper::getFiles()
319315
boost::python::list NotepadPlusWrapper::getSessionFiles(const char *sessionFilename)
320316
{
321317
boost::python::list result;
322-
#ifdef UNICODE
323318
std::shared_ptr<TCHAR> converted = WcharMbcsConverter::char2tchar(sessionFilename);
324319
const TCHAR *convertedSessionFilename = converted.get();
325-
#else
326-
const TCHAR *convertedSessionFilename = sessionFilename;
327-
#endif
328320
idx_t count = (idx_t)callNotepad(NPPM_GETNBSESSIONFILES, 0, reinterpret_cast<LPARAM>(convertedSessionFilename));
329321
if (count > 0)
330322
{
@@ -339,12 +331,8 @@ boost::python::list NotepadPlusWrapper::getSessionFiles(const char *sessionFilen
339331

340332
for(idx_t pos = 0; pos < count; pos++)
341333
{
342-
#ifdef UNICODE
343-
std::shared_ptr<char> mbFilename = WcharMbcsConverter::tchar2char(fileNames[pos]);
344-
result.append(const_cast<const char*>(mbFilename.get()));
345-
#else
346-
result.append(const_cast<const char*>(fileNames[pos]));
347-
#endif
334+
std::shared_ptr<char> mbFilename = WcharMbcsConverter::tchar2char(fileNames[pos]);
335+
result.append(const_cast<const char*>(mbFilename.get()));
348336
}
349337

350338
}
@@ -424,13 +412,8 @@ idx_t NotepadPlusWrapper::getCurrentDocIndex(int view)
424412

425413
void NotepadPlusWrapper::setStatusBar(StatusBarSection section, const char *text)
426414
{
427-
#ifdef UNICODE
428415
std::shared_ptr<TCHAR> s = WcharMbcsConverter::char2tchar(text);
429416
callNotepad(NPPM_SETSTATUSBAR, static_cast<WPARAM>(section), reinterpret_cast<LPARAM>(s.get()));
430-
#else
431-
callNotepad(NPPM_SETSTATUSBAR, static_cast<WPARAM>(section), reinterpret_cast<LPARAM>(text));
432-
#endif
433-
434417
}
435418

436419
intptr_t NotepadPlusWrapper::getPluginMenuHandle()
@@ -448,36 +431,19 @@ void NotepadPlusWrapper::loadSession(boost::python::str filename)
448431
{
449432
notAllowedInScintillaCallback("loadSession() cannot be called in a synchronous editor callback. "
450433
"Use an asynchronous callback, or avoid using loadSession() in the callback handler");
451-
#ifdef UNICODE
452-
std::shared_ptr<TCHAR> s = WcharMbcsConverter::char2tchar((const char*)boost::python::extract<const char*>(filename));
453-
callNotepad(NPPM_LOADSESSION, 0, reinterpret_cast<LPARAM>(s.get()));
454-
#else
455-
callNotepad(NPPM_LOADSESSION, 0, reinterpret_cast<LPARAM>((const char*)boost::python::extract<const char*>(filename)));
456-
#endif
457-
434+
handleFileNameToLongPath(NPPM_LOADSESSION, filename);
458435
}
459436

460437
bool NotepadPlusWrapper::activateFileString(boost::python::str filename)
461438
{
462439
notAllowedInScintillaCallback("activateFile() cannot be called in a synchronous editor callback. "
463440
"Use an asynchronous callback, or avoid using activateFile() in the callback handler");
464-
#ifdef UNICODE
465-
std::shared_ptr<TCHAR> s = WcharMbcsConverter::char2tchar((const char*)boost::python::extract<const char*>(filename));
466-
return static_cast<bool>(callNotepad(NPPM_SWITCHTOFILE, 0, reinterpret_cast<LPARAM>(s.get())));
467-
#else
468-
return static_cast<bool>(callNotepad(NPPM_SWITCHTOFILE, 0, reinterpret_cast<LPARAM>((const char*)boost::python::extract<const char*>(filename))));
469-
#endif
470-
441+
return handleFileNameToLongPath(NPPM_SWITCHTOFILE, filename);
471442
}
472443

473444
bool NotepadPlusWrapper::reloadFile(boost::python::str filename, bool alert)
474445
{
475-
#ifdef UNICODE
476-
return static_cast<bool>(callNotepad(NPPM_RELOADFILE, alert ? 1 : 0, reinterpret_cast<LPARAM>(static_cast<const TCHAR *>(WcharMbcsConverter::char2tchar(boost::python::extract<const char *>(filename)).get()))));
477-
#else
478-
return static_cast<bool>(callNotepad(NPPM_RELOADFILE, alert ? 1 : 0, reinterpret_cast<LPARAM>(static_cast<const char *>(boost::python::extract<const char *>(filename)))));
479-
#endif
480-
446+
return handleFileNameToLongPath(NPPM_RELOADFILE, filename, alert ? 1 : 0);
481447
}
482448

483449
bool NotepadPlusWrapper::saveAllFiles()
@@ -956,8 +922,7 @@ bool NotepadPlusWrapper::isStatusBarHidden()
956922

957923
bool NotepadPlusWrapper::saveFile(boost::python::str filename)
958924
{
959-
std::shared_ptr<TCHAR> s = WcharMbcsConverter::char2tchar((const char*)boost::python::extract<const char*>(filename));
960-
return static_cast<bool>(callNotepad(NPPM_SAVEFILE, 0, reinterpret_cast<LPARAM>(s.get())));
925+
return handleFileNameToLongPath(NPPM_SAVEFILE, filename);
961926
}
962927

963928
void NotepadPlusWrapper::showDocSwitcher(bool showOrNot)
@@ -1120,4 +1085,32 @@ void NotepadPlusWrapper::invalidValueProvided(const char *message)
11201085
throw InvalidValueProvidedException(message);
11211086
}
11221087

1088+
bool NotepadPlusWrapper::handleFileNameToLongPath(UINT nppmID, boost::python::str filename, WPARAM wParam)
1089+
{
1090+
std::shared_ptr<TCHAR> s = WcharMbcsConverter::char2tchar((const char*)boost::python::extract<const char*>(filename));
1091+
1092+
const DWORD longPathBufferSize = ::GetLongPathName(s.get(), nullptr, 0);
1093+
if (longPathBufferSize == 0)
1094+
{
1095+
// Handle an error condition.
1096+
DEBUG_TRACE((L"GetLongPathName get size failed (%d)\n", GetLastError()));
1097+
return false;
1098+
}
1099+
else
1100+
{
1101+
std::vector<TCHAR> longNameFullpath(longPathBufferSize);
1102+
1103+
if (::GetLongPathName(s.get(), longNameFullpath.data(), static_cast<DWORD>(longNameFullpath.size())) != (longPathBufferSize - 1))
1104+
{
1105+
// Handle an error condition.
1106+
DEBUG_TRACE((L"GetLongPathName get path failed (%d)\n", GetLastError()));
1107+
return false;
1108+
}
1109+
else
1110+
{
1111+
return static_cast<bool>(callNotepad(nppmID, wParam, reinterpret_cast<LPARAM>(longNameFullpath.data())));
1112+
}
1113+
}
1114+
}
1115+
11231116
}

PythonScript/src/NotepadPlusWrapper.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,6 @@ class NotepadPlusWrapper
703703

704704
LangType getBufferLangType(intptr_t bufferID);
705705

706-
void setLangType(LangType language);
707706

708707
void setBufferLangType(LangType lang, intptr_t bufferID);
709708

@@ -853,7 +852,7 @@ class NotepadPlusWrapper
853852
void notAllowedInScintillaCallback(const char *message);
854853
bool checkForValidBuffer(intptr_t bufferID);
855854
void invalidValueProvided(const char *message);
856-
static void runCallbacks(NppPythonScript::CallbackExecArgs *args);
855+
bool handleFileNameToLongPath(UINT nppmID, boost::python::str filename, WPARAM wParam = 0);
857856
};
858857
}
859858
#endif

0 commit comments

Comments
 (0)