From 453e13a116ba356df67595fe3f7305f4de8c8ded Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 14:22:04 +0700 Subject: [PATCH 1/4] test(windows): make a new DuckDB and SQLite file through the save picker and connect --- core/crates/drivers/duckdb/src/create_file.rs | 42 +++++++--- core/crates/drivers/sqlite/src/create_file.rs | 10 ++- .../TablePro.App.UITests/NewFileTests.cs | 80 +++++++++++++++++++ .../TablePro.Interop.Tests/NewFileTests.cs | 42 ++++++++++ 4 files changed, 160 insertions(+), 14 deletions(-) create mode 100644 windows/tests/TablePro.App.UITests/NewFileTests.cs create mode 100644 windows/tests/TablePro.Interop.Tests/NewFileTests.cs diff --git a/core/crates/drivers/duckdb/src/create_file.rs b/core/crates/drivers/duckdb/src/create_file.rs index 00f467c6b6..f3efdcdd65 100644 --- a/core/crates/drivers/duckdb/src/create_file.rs +++ b/core/crates/drivers/duckdb/src/create_file.rs @@ -187,12 +187,31 @@ fn wal_of(path: &Path) -> PathBuf { } #[cfg(test)] -#[cfg(unix)] mod tests { - use std::os::unix::fs::PermissionsExt; - use super::*; + fn tables_in(path: &Path) -> i64 { + let reopened = duckdb::Connection::open(path).unwrap(); + reopened + .query_row("SELECT count(*) FROM duckdb_tables()", [], |row| row.get(0)) + .unwrap() + } + + #[test] + fn a_new_database_is_made_where_no_file_was() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("new.duckdb"); + + create_or_replace(&path, &CancellationToken::new()).unwrap(); + + let made: Vec<_> = std::fs::read_dir(directory.path()) + .unwrap() + .map(|entry| entry.unwrap().file_name()) + .collect(); + assert_eq!(made, [std::ffi::OsString::from("new.duckdb")]); + assert_eq!(tables_in(&path), 0); + } + #[test] fn an_existing_file_and_its_log_are_replaced_by_an_empty_private_database() { let directory = tempfile::tempdir().unwrap(); @@ -202,8 +221,13 @@ mod tests { create_or_replace(&path, &CancellationToken::new()).unwrap(); - let metadata = std::fs::metadata(&path).unwrap(); - assert_eq!(metadata.permissions().mode() & 0o777, 0o600); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + + let metadata = std::fs::metadata(&path).unwrap(); + assert_eq!(metadata.permissions().mode() & 0o777, 0o600); + } assert!( !wal_of(&path).exists(), "the old log would be replayed into the new database" @@ -213,11 +237,7 @@ mod tests { .map(|entry| entry.unwrap().file_name()) .collect(); assert_eq!(leftovers, [std::ffi::OsString::from("data.duckdb")]); - let reopened = duckdb::Connection::open(&path).unwrap(); - let tables: i64 = reopened - .query_row("SELECT count(*) FROM duckdb_tables()", [], |row| row.get(0)) - .unwrap(); - assert_eq!(tables, 0); + assert_eq!(tables_in(&path), 0); } #[test] @@ -246,6 +266,8 @@ mod tests { ); } + // Windows lists no process's handles, so there only a claim says a file is held. + #[cfg(unix)] #[test] fn a_file_this_process_holds_is_not_replaced() { let directory = tempfile::tempdir().unwrap(); diff --git a/core/crates/drivers/sqlite/src/create_file.rs b/core/crates/drivers/sqlite/src/create_file.rs index 247e756ec0..8283c0f09b 100644 --- a/core/crates/drivers/sqlite/src/create_file.rs +++ b/core/crates/drivers/sqlite/src/create_file.rs @@ -76,10 +76,7 @@ fn file_error(kind: ErrorKind, path: &Path) -> DriverError { } #[cfg(test)] -#[cfg(unix)] mod tests { - use std::os::unix::fs::PermissionsExt; - use super::*; #[test] @@ -92,7 +89,12 @@ mod tests { let metadata = std::fs::metadata(&path).unwrap(); assert_eq!(metadata.len(), 0); - assert_eq!(metadata.permissions().mode() & 0o777, 0o600); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + + assert_eq!(metadata.permissions().mode() & 0o777, 0o600); + } let leftovers: Vec<_> = std::fs::read_dir(directory.path()) .unwrap() .map(|entry| entry.unwrap().file_name()) diff --git a/windows/tests/TablePro.App.UITests/NewFileTests.cs b/windows/tests/TablePro.App.UITests/NewFileTests.cs new file mode 100644 index 0000000000..7916a9ebd9 --- /dev/null +++ b/windows/tests/TablePro.App.UITests/NewFileTests.cs @@ -0,0 +1,80 @@ +using FlaUI.Core.AutomationElements; +using FlaUI.Core.Definitions; +using FlaUI.Core.Input; +using FlaUI.Core.Tools; +using FlaUI.Core.WindowsAPI; + +namespace TablePro.App.UITests; + +public sealed partial class ShellTests +{ + public static TheoryData ThemesAndFileEngines => new() + { + { "Light", "duckdb" }, + { "Dark", "duckdb" }, + { "Light", "sqlite" }, + { "Dark", "sqlite" }, + }; + + [Theory] + [MemberData(nameof(ThemesAndFileEngines))] + public void ANewFileNamedInTheSavePickerIsMadeAndConnects(string theme, string engine) + { + using var app = AppSession.Launch(theme); + var file = Path.Combine(app.DataFolder, $"made.{engine}"); + var name = $"New {engine} file"; + app.Click(app.Find("NewConnectionCard"), () => app.HasWindow("New connection")); + var form = app.WaitForWindow("New connection"); + app.Click(app.Find($"Engine_{engine}", form), () => app.TryFind("FileBox", form) is not null, form); + app.Find("NameBox", form).AsTextBox().Text = name; + app.Click(app.Find("NewFileButton", form), () => SaveDialog(app, form) is not null, form); + SaveInFileDialog(app, form, file); + // The picker hands back the long form of a folder typed with an 8.3 name, as the runner's temp folder can be. + Assert.Equal(Path.GetFileName(file), Path.GetFileName(app.Find("FileBox", form).AsTextBox().Text)); + Assert.False(File.Exists(file), "the picker made the file before Connect"); + app.Screenshot($"109-new-{engine}-file-named", form); + + app.Find("ConnectButton", form).AsButton().Invoke(); + var status = app.Find("FormStatus", form); + Assert.True( + Retry.WhileFalse(() => !app.HasWindow("New connection"), AppSession.Wait, throwOnTimeout: false).Success, + "The form stayed open: " + AppSession.TextOf(status)); + + Assert.True(File.Exists(file)); + Assert.False(app.Find($"Rail_{name}").IsOffscreen); + app.Find("ObjectTree"); + app.Screenshot($"110-new-{engine}-file-connected"); + } + + // The form owns its picker, so UI Automation lists it under the form, the app's window or the desktop. + private static Window? SaveDialog(AppSession app, Window form) => + form.FindFirstChild(condition => condition.ByClassName("#32770"))?.AsWindow() ?? FileDialog(app); + + // The Save dialog's File name box is control 1001, and Enter saves to the path typed there. + private static void SaveInFileDialog(AppSession app, Window form, string path) + { + var dialog = Retry.WhileNull(() => SaveDialog(app, form), AppSession.Wait, throwOnTimeout: false).Result + ?? throw new InvalidOperationException("The Save dialog did not appear."); + var box = Retry.WhileNull( + () => dialog.FindFirstDescendant(condition => + condition.ByAutomationId("1001").And(condition.ByControlType(ControlType.Edit))), + AppSession.Wait, + throwOnTimeout: false).Result + ?? throw new InvalidOperationException("The Save dialog has no File name box."); + // The Save dialog keeps its suggested name when the box's value is set without typing, so the path is typed. + dialog.SetForeground(); + box.Focus(); + Keyboard.TypeSimultaneously(VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_A); + Keyboard.Type(path); + if (!Retry.WhileFalse(() => box.AsTextBox().Text == path, AppSession.Wait, throwOnTimeout: false).Success) + { + throw new InvalidOperationException("The Save dialog did not take the path typed."); + } + + Keyboard.Type(VirtualKeyShort.RETURN); + if (!Retry.WhileFalse(() => SaveDialog(app, form) is null, AppSession.Wait, throwOnTimeout: false).Success) + { + throw new InvalidOperationException("The Save dialog did not take the path."); + } + } +} diff --git a/windows/tests/TablePro.Interop.Tests/NewFileTests.cs b/windows/tests/TablePro.Interop.Tests/NewFileTests.cs new file mode 100644 index 0000000000..6e375e34f8 --- /dev/null +++ b/windows/tests/TablePro.Interop.Tests/NewFileTests.cs @@ -0,0 +1,42 @@ +namespace TablePro.Interop.Tests; + +public sealed class NewFileTests : IDisposable +{ + private readonly string _directory = Directory.CreateTempSubdirectory("tablepro-new-file-").FullName; + private readonly TableProCore _core = TableProCore.Create(new HostConfiguration("test"), new MemorySecretStore()); + + public void Dispose() + { + _core.Dispose(); + try + { + Directory.Delete(_directory, recursive: true); + } + catch (IOException) + { + // The database closes on the core's own threads, which can still hold it here on Windows. + } + } + + // As the connection form's New file does: make the file, close it, then connect to it as saved. + [Theory] + [InlineData("duckdb", "made.duckdb")] + [InlineData("sqlite", "made.sqlite")] + public async Task ANewFileIsMadeEmptyAndOpensAgainForWriting(string engine, string name) + { + var cancellationToken = TestContext.Current.CancellationToken; + var path = Path.Combine(_directory, name); + + var made = await _core.OpenFileAsync(new FileOpenOptions(path, engine, Create: true), cancellationToken); + await made.DisposeAsync(); + + Assert.True(File.Exists(path)); + Assert.DoesNotContain(Directory.GetFiles(_directory), file => Path.GetFileName(file).StartsWith('.')); + await using var session = await _core.OpenFileAsync(new FileOpenOptions(path, engine), cancellationToken); + Assert.Equal(engine, session.EngineId); + Assert.Empty(await session.RefreshObjectsAsync(ObjectScope.DefaultSchema, cancellationToken)); + await using var editor = await session.OpenEditorAsync(cancellationToken); + using var run = editor.Run("CREATE TABLE notes (body TEXT)"); + Assert.IsType(await run.Completion); + } +} From fb06fc89aea9f7f0b049afe55532f5a0aa340205 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 14:30:24 +0700 Subject: [PATCH 2/4] fix(windows): flush a new DuckDB file through a handle open for writing, and open a file New file names that exists as it is --- core/crates/drivers/duckdb/src/create_file.rs | 5 +++- .../drivers/duckdb/src/result_stream.rs | 2 ++ .../ConnectionFormWindow.xaml.cs | 6 +++-- .../TablePro.App/Strings/en-US/Resources.resw | 3 +++ .../TablePro.App.UITests/NewFileTests.cs | 25 ++++++++++++++++++- .../TablePro.Interop.Tests/NewFileTests.cs | 2 +- 6 files changed, 38 insertions(+), 5 deletions(-) diff --git a/core/crates/drivers/duckdb/src/create_file.rs b/core/crates/drivers/duckdb/src/create_file.rs index f3efdcdd65..c659b20776 100644 --- a/core/crates/drivers/duckdb/src/create_file.rs +++ b/core/crates/drivers/duckdb/src/create_file.rs @@ -78,7 +78,10 @@ fn build_empty(temporary: &Path, target: &Path) -> Result<(), DriverError> { std::fs::set_permissions(temporary, std::fs::Permissions::from_mode(0o600)) .map_err(|error| file_error(error.kind(), target))?; } - File::open(temporary) + // Windows flushes only a handle opened for writing. + OpenOptions::new() + .write(true) + .open(temporary) .and_then(|file| file.sync_all()) .map_err(|error| file_error(error.kind(), target)) } diff --git a/core/crates/drivers/duckdb/src/result_stream.rs b/core/crates/drivers/duckdb/src/result_stream.rs index bfaaefad38..c507ab74cc 100644 --- a/core/crates/drivers/duckdb/src/result_stream.rs +++ b/core/crates/drivers/duckdb/src/result_stream.rs @@ -925,6 +925,8 @@ mod tests { /// Measured against DuckDB 1.5: a missing part is passed by its text, /// a link is followed before the `..` after it. + // Windows makes a symbolic link only in Developer Mode or with an administrator's right. + #[cfg(unix)] #[test] fn a_path_is_resolved_as_duckdb_opens_it() { let root = tempfile::tempdir().unwrap(); diff --git a/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs b/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs index 5070dc9d5e..bbaef01de6 100644 --- a/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs +++ b/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs @@ -623,6 +623,8 @@ private async void OnNewFile(object sender, RoutedEventArgs e) { SuggestedStartLocation = PickerLocationId.DocumentsLibrary, SuggestedFileName = Strings.Get("NewDatabaseFileName"), + // A file that is there already is opened as it is, never replaced, so the picker does not ask to replace it. + ShowOverwritePrompt = false, }; if (suffixes.Count > 0) { @@ -633,8 +635,8 @@ private async void OnNewFile(object sender, RoutedEventArgs e) if (result is not null) { FileBox.Text = result.Path; - _createFile = true; - SetStatus(Strings.Get("NewFileOnConnect"), null); + _createFile = !File.Exists(result.Path); + SetStatus(Strings.Get(_createFile ? "NewFileOnConnect" : "NewFileExists"), null); } } diff --git a/windows/src/TablePro.App/Strings/en-US/Resources.resw b/windows/src/TablePro.App/Strings/en-US/Resources.resw index b48928f7a3..aa7a62db66 100644 --- a/windows/src/TablePro.App/Strings/en-US/Resources.resw +++ b/windows/src/TablePro.App/Strings/en-US/Resources.resw @@ -1002,6 +1002,9 @@ The file is made when you connect + + This file exists, so it opens as it is + Testing... diff --git a/windows/tests/TablePro.App.UITests/NewFileTests.cs b/windows/tests/TablePro.App.UITests/NewFileTests.cs index 7916a9ebd9..d0ff3f3442 100644 --- a/windows/tests/TablePro.App.UITests/NewFileTests.cs +++ b/windows/tests/TablePro.App.UITests/NewFileTests.cs @@ -32,10 +32,11 @@ public void ANewFileNamedInTheSavePickerIsMadeAndConnects(string theme, string e // The picker hands back the long form of a folder typed with an 8.3 name, as the runner's temp folder can be. Assert.Equal(Path.GetFileName(file), Path.GetFileName(app.Find("FileBox", form).AsTextBox().Text)); Assert.False(File.Exists(file), "the picker made the file before Connect"); + var status = app.Find("FormStatus", form); + Assert.Equal("The file is made when you connect", AppSession.WaitForText(status, text => text.Length > 0)); app.Screenshot($"109-new-{engine}-file-named", form); app.Find("ConnectButton", form).AsButton().Invoke(); - var status = app.Find("FormStatus", form); Assert.True( Retry.WhileFalse(() => !app.HasWindow("New connection"), AppSession.Wait, throwOnTimeout: false).Success, "The form stayed open: " + AppSession.TextOf(status)); @@ -46,6 +47,28 @@ public void ANewFileNamedInTheSavePickerIsMadeAndConnects(string theme, string e app.Screenshot($"110-new-{engine}-file-connected"); } + [Theory] + [MemberData(nameof(Themes))] + public void AnExistingFileNamedForANewOneOpensAsItIsWithoutAReplaceQuestion(string theme) + { + using var app = AppSession.Launch(theme); + var file = AppSession.SampleDatabase(app.DataFolder); + app.Click(app.Find("NewConnectionCard"), () => app.HasWindow("New connection")); + var form = app.WaitForWindow("New connection"); + app.Click(app.Find("Engine_sqlite", form), () => app.TryFind("FileBox", form) is not null, form); + app.Find("NameBox", form).AsTextBox().Text = "Kept"; + app.Click(app.Find("NewFileButton", form), () => SaveDialog(app, form) is not null, form); + SaveInFileDialog(app, form, file); + Assert.Equal( + "This file exists, so it opens as it is", + AppSession.WaitForText(app.Find("FormStatus", form), text => text.Length > 0)); + app.Screenshot("111-new-file-exists", form); + + app.Find("ConnectButton", form).AsButton().Invoke(); + app.WaitForClose("New connection"); + app.FindByName("Album", app.Find("ObjectTree")); + } + // The form owns its picker, so UI Automation lists it under the form, the app's window or the desktop. private static Window? SaveDialog(AppSession app, Window form) => form.FindFirstChild(condition => condition.ByClassName("#32770"))?.AsWindow() ?? FileDialog(app); diff --git a/windows/tests/TablePro.Interop.Tests/NewFileTests.cs b/windows/tests/TablePro.Interop.Tests/NewFileTests.cs index 6e375e34f8..76b132b94f 100644 --- a/windows/tests/TablePro.Interop.Tests/NewFileTests.cs +++ b/windows/tests/TablePro.Interop.Tests/NewFileTests.cs @@ -36,7 +36,7 @@ public async Task ANewFileIsMadeEmptyAndOpensAgainForWriting(string engine, stri Assert.Equal(engine, session.EngineId); Assert.Empty(await session.RefreshObjectsAsync(ObjectScope.DefaultSchema, cancellationToken)); await using var editor = await session.OpenEditorAsync(cancellationToken); - using var run = editor.Run("CREATE TABLE notes (body TEXT)"); + using var run = editor.Run("CREATE TABLE notes (body TEXT)", cancellationToken: cancellationToken); Assert.IsType(await run.Completion); } } From c78714a09c9af3643d63bd84e3c6b0918d3d2894 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 14:33:56 +0700 Subject: [PATCH 3/4] test(windows): read the form's status only while the form is open --- windows/tests/TablePro.App.UITests/NewFileTests.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/windows/tests/TablePro.App.UITests/NewFileTests.cs b/windows/tests/TablePro.App.UITests/NewFileTests.cs index d0ff3f3442..1376d29c93 100644 --- a/windows/tests/TablePro.App.UITests/NewFileTests.cs +++ b/windows/tests/TablePro.App.UITests/NewFileTests.cs @@ -37,9 +37,11 @@ public void ANewFileNamedInTheSavePickerIsMadeAndConnects(string theme, string e app.Screenshot($"109-new-{engine}-file-named", form); app.Find("ConnectButton", form).AsButton().Invoke(); - Assert.True( - Retry.WhileFalse(() => !app.HasWindow("New connection"), AppSession.Wait, throwOnTimeout: false).Success, - "The form stayed open: " + AppSession.TextOf(status)); + // The status is read only while the form is still open, since a closed form's elements are gone. + if (!Retry.WhileFalse(() => !app.HasWindow("New connection"), AppSession.Wait, throwOnTimeout: false).Success) + { + Assert.Fail("The form stayed open: " + AppSession.TextOf(status)); + } Assert.True(File.Exists(file)); Assert.False(app.Find($"Rail_{name}").IsOffscreen); From 6168f2f0bcbd242d150936f7e6368e8d60078720 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 14:34:44 +0700 Subject: [PATCH 4/4] test(windows): wait for the picked path to reach the form --- windows/tests/TablePro.App.UITests/NewFileTests.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/windows/tests/TablePro.App.UITests/NewFileTests.cs b/windows/tests/TablePro.App.UITests/NewFileTests.cs index 1376d29c93..6bdb82b5d3 100644 --- a/windows/tests/TablePro.App.UITests/NewFileTests.cs +++ b/windows/tests/TablePro.App.UITests/NewFileTests.cs @@ -29,8 +29,11 @@ public void ANewFileNamedInTheSavePickerIsMadeAndConnects(string theme, string e app.Find("NameBox", form).AsTextBox().Text = name; app.Click(app.Find("NewFileButton", form), () => SaveDialog(app, form) is not null, form); SaveInFileDialog(app, form, file); - // The picker hands back the long form of a folder typed with an 8.3 name, as the runner's temp folder can be. - Assert.Equal(Path.GetFileName(file), Path.GetFileName(app.Find("FileBox", form).AsTextBox().Text)); + // The path reaches the form after the dialog closes, with a folder typed by its 8.3 name in its long form. + var fileBox = app.Find("FileBox", form).AsTextBox(); + Assert.True( + Retry.WhileFalse(() => Path.GetFileName(fileBox.Text) == Path.GetFileName(file), AppSession.Wait, throwOnTimeout: false).Success, + "The form shows " + fileBox.Text); Assert.False(File.Exists(file), "the picker made the file before Connect"); var status = app.Find("FormStatus", form); Assert.Equal("The file is made when you connect", AppSession.WaitForText(status, text => text.Length > 0));