diff --git a/example/config_example.dart b/example/config_example.dart index 9c62342..18505b8 100644 --- a/example/config_example.dart +++ b/example/config_example.dart @@ -1,53 +1,42 @@ import 'package:libgit2dart/libgit2dart.dart'; void main() { - final repoConfig = Config(path: '.git/config'); + // Open system + global config file. + final config = Config.open(); - print('All entries of repo config:'); - for (final entry in repoConfig.entries.entries) { + print('All entries of system/global config:'); + // config.variables hold key/value from config file + for (final entry in config.variables.entries) { print('${entry.key}: ${entry.value}'); } + // .close should be called on object to free memory when done. + config.close(); - repoConfig.close(); - + // Open config file at provided path. + // Exception is thrown if file not found. try { - final systemConfig = Config.system(); + final repoConfig = Config.open(path: '.git/config'); - print( - '\nUser Name from system config: ${systemConfig.entries['user.name']}'); + print('All entries of repo config:'); + for (final entry in repoConfig.variables.entries) { + print('${entry.key}: ${entry.value}'); + } - systemConfig.close(); + repoConfig.close(); } catch (e) { - print('\n$e'); + print(e); } + // Open global config file if there's one. + // Exception is thrown if file not found. try { final globalConfig = Config.global(); - print( - '\nUser Name from global config: ${globalConfig.entries['user.name']}'); + final userName = globalConfig.variables['user.name']; + print('\nUser Name from global config: $userName'); globalConfig.close(); } catch (e) { print('\n$e'); } - - try { - final xdgConfig = Config.xdg(); - print('\nAll entries of repo config:'); - - print('\nUser Name from xdg config: ${xdgConfig.entries['user.name']}'); - - xdgConfig.close(); - } catch (e) { - print('\n$e'); - } - - final config = Config(); - - print('\nAll entries of system/global config:'); - for (final entry in config.entries.entries) { - print('${entry.key}: ${entry.value}'); - } - config.close(); } diff --git a/lib/src/bindings/config.dart b/lib/src/bindings/config.dart index 939fa5a..02715af 100644 --- a/lib/src/bindings/config.dart +++ b/lib/src/bindings/config.dart @@ -169,12 +169,6 @@ int getInt(Pointer cfg, String variable) { return result; } -/// Get the value of a config variable and parse it as a string. -String getString(Pointer cfg, String variable) { - final value = getConfigValue(cfg, variable); - return value.cast().toDartString(); -} - /// Set the value of a boolean config variable in the config file with the /// highest level (usually the local one). /// @@ -192,6 +186,8 @@ void setBool(Pointer cfg, String name, bool value) { /// Set the value of an integer config variable in the config file with the /// highest level (usually the local one). +/// +/// Throws a [LibGit2Error] if error occured. void setInt(Pointer cfg, String name, int value) { final nameC = name.toNativeUtf8().cast(); final error = libgit2.git_config_set_int64(cfg, nameC, value); @@ -204,6 +200,8 @@ void setInt(Pointer cfg, String name, int value) { /// Set the value of a string config variable in the config file with the /// highest level (usually the local one). +/// +/// Throws a [LibGit2Error] if error occured. void setString(Pointer cfg, String name, String value) { final nameC = name.toNativeUtf8().cast(); final valueC = value.toNativeUtf8().cast(); diff --git a/lib/src/config.dart b/lib/src/config.dart index bed7237..6858077 100644 --- a/lib/src/config.dart +++ b/lib/src/config.dart @@ -114,7 +114,7 @@ class Config { Map variables = {}; /// Sets value of config key - void setEntry(String key, dynamic value) { + void setVariable(String key, dynamic value) { try { if (value.runtimeType == bool) { config.setBool(configPointer.value, key, value); diff --git a/test/config_test.dart b/test/config_test.dart index 34b2906..8dff9dc 100644 --- a/test/config_test.dart +++ b/test/config_test.dart @@ -5,62 +5,52 @@ import 'package:libgit2dart/src/util.dart'; import 'package:libgit2dart/src/config.dart'; void main() { + final tmpDir = Directory.systemTemp.path; const configFileName = 'test_config'; + late Config config; + group('Config', () { setUpAll(() { libgit2.git_libgit2_init(); }); setUp(() { - File('${Directory.current.path}/test/$configFileName').writeAsStringSync( + File('$tmpDir/$configFileName').writeAsStringSync( '[core]\n\trepositoryformatversion = 0\n\tbare = false\n[remote "origin"]\n\turl = someurl'); + config = Config.open(path: '$tmpDir/$configFileName'); }); tearDown(() { - File('${Directory.current.path}/test/$configFileName').deleteSync(); + config.close(); + File('$tmpDir/$configFileName').deleteSync(); }); tearDownAll(() { libgit2.git_libgit2_shutdown(); }); - test('opens file successfully', () { - final config = Config(); - expect(config, isA()); - config.close(); - }); - test('opens file successfully with provided path', () { - final config = Config(path: 'test/$configFileName'); expect(config, isA()); - config.close(); }); test('gets entries of file', () { - final config = Config(path: 'test/$configFileName'); - expect(config.entries['core.repositoryformatversion'], equals('0')); - config.close(); + expect(config.variables['core.repositoryformatversion'], equals('0')); }); test('sets boolean value for provided key', () { - final config = Config(path: 'test/$configFileName'); - config.setEntry('core.bare', true); - expect(config.entries['core.bare'], equals('true')); - config.close(); + config.setVariable('core.bare', true); + expect(config.variables['core.bare'], equals('true')); }); test('sets integer value for provided key', () { - final config = Config(path: 'test/$configFileName'); - config.setEntry('core.repositoryformatversion', 1); - expect(config.entries['core.repositoryformatversion'], equals('1')); - config.close(); + config.setVariable('core.repositoryformatversion', 1); + expect(config.variables['core.repositoryformatversion'], equals('1')); }); test('sets string value for provided key', () { - final config = Config(path: 'test/$configFileName'); - config.setEntry('remote.origin.url', 'updated'); - expect(config.entries['remote.origin.url'], equals('updated')); - config.close(); + config.setVariable('remote.origin.url', 'updated'); + expect(config.variables['remote.origin.url'], equals('updated')); }); }); + ; }