Skip to content

Commit

Permalink
fix: don't require trailing path separator when copying databases
Browse files Browse the repository at this point in the history
Fixes #444
  • Loading branch information
blaugold committed Dec 16, 2022
1 parent a306fac commit d5098af
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
21 changes: 19 additions & 2 deletions packages/cbl/lib/src/database/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import 'dart:async';

import 'package:meta/meta.dart';
import 'package:path/path.dart' as _path;

import '../document/blob.dart';
import '../document/document.dart';
Expand Down Expand Up @@ -233,7 +234,11 @@ abstract class Database implements ClosableResource {
required String name,
DatabaseConfiguration? config,
}) =>
AsyncDatabase.copy(from: from, name: name, config: config);
AsyncDatabase.copy(
from: _formatCopyFromPath(from),
name: name,
config: config,
);

/// {@template cbl.Database.copySync}
/// Copies a canned database [from] the given path to a new database with the
Expand All @@ -247,7 +252,19 @@ abstract class Database implements ClosableResource {
required String name,
DatabaseConfiguration? config,
}) =>
SyncDatabase.copy(from: from, name: name, config: config);
SyncDatabase.copy(
from: _formatCopyFromPath(from),
name: name,
config: config,
);

static String _formatCopyFromPath(String from) =>
// TODO(blaugold): remove workaround once the CBL C SDK is fixed
// Ensure that the path ends with a separator to signal that it is a
// directory.
// This is currently required by the native implementation.
// https://github.com/cbl-dart/cbl-dart/issues/444
Uri.parse(from).pathSegments.last.isEmpty ? from : _path.join(from, '');

/// Configuration of the [ConsoleLogger], [FileLogger] and a custom [Logger].
static final Log log = LogImpl();
Expand Down
1 change: 1 addition & 0 deletions packages/cbl/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies:
collection: ^1.15.0
ffi: ^2.0.1
meta: ^1.3.0
path: ^1.8.0
stream_channel: ^2.1.0
synchronized: ^3.0.0
web_socket_channel: ^2.1.0
Expand Down
17 changes: 17 additions & 0 deletions packages/cbl_e2e_tests/lib/src/database/database_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:typed_data';

import 'package:cbl/cbl.dart';
import 'package:cbl/src/support/utils.dart';
import 'package:path/path.dart' as path;

import '../../test_binding_impl.dart';
import '../test_binding.dart';
Expand Down Expand Up @@ -53,6 +54,22 @@ void main() {
expect(await databaseExists('copy', directory: directory), isTrue);
});

apiTest('copy without trailing path separator in from path', () async {
// https://github.com/cbl-dart/cbl-dart/issues/444
final source = await openTestDatabase();
final directory = databaseDirectoryForTest();

expect(source.path, endsWith(path.separator));

await copyDatabase(
from: source.path!.substring(0, source.path!.length - 1),
name: 'copy',
config: DatabaseConfiguration(directory: directory),
);

expect(await databaseExists('copy', directory: directory), isTrue);
});

apiTest('open in default directory', () async {
final name = createUuid();
final defaultConfig = DatabaseConfiguration();
Expand Down

0 comments on commit d5098af

Please sign in to comment.