2017-09-07 22:02:51 +03:00
#!/usr/bin/env gjs
//
// Copyright (C) 2013 Colin Walters <walters@verbum.org>
// Copyright (C) 2017 Dan Nicholson <nicholson@endlessm.com>
//
2018-01-30 22:26:26 +03:00
// SPDX-License-Identifier: LGPL-2.0+
//
2017-09-07 22:02:51 +03:00
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
const GLib = imports . gi . GLib ;
const Gio = imports . gi . Gio ;
const OSTree = imports . gi . OSTree ;
function assertEquals ( a , b ) {
if ( a != b )
throw new Error ( "assertion failed " + JSON . stringify ( a ) + " == " + JSON . stringify ( b ) ) ;
}
function assertNotEquals ( a , b ) {
if ( a == b )
throw new Error ( "assertion failed " + JSON . stringify ( a ) + " != " + JSON . stringify ( b ) ) ;
}
2017-09-12 20:23:31 +03:00
print ( '1..9' )
2017-09-07 22:02:51 +03:00
let remotesDir = Gio . File . new _for _path ( 'remotes.d' ) ;
remotesDir . make _directory ( null ) ;
let remoteConfig = GLib . KeyFile . new ( )
remoteConfig . set _value ( 'remote "foo"' , 'url' , 'http://foo' )
let remoteConfigFile = remotesDir . get _child ( 'foo.conf' )
remoteConfig . save _to _file ( remoteConfigFile . get _path ( ) )
2017-09-12 20:23:31 +03:00
let remoteOptions = GLib . Variant . new ( 'a{sv}' , {
'branches' : GLib . Variant . new ( 'as' , [ 'test' ] ) ,
} ) ;
2017-09-07 22:02:51 +03:00
// Use the full Repo constructor to set remotes-config-dir
let repoFile = Gio . File . new _for _path ( 'repo' ) ;
let repo = new OSTree . Repo ( { path : repoFile ,
remotes _config _dir : remotesDir . get _path ( ) } ) ;
repo . create ( OSTree . RepoMode . ARCHIVE _Z2 , null ) ;
repo . open ( null ) ;
// See if the remotes.d remote exists
let remotes = repo . remote _list ( )
assertNotEquals ( remotes . indexOf ( 'foo' ) , - 1 ) ;
print ( "ok read-remotes-config-dir" ) ;
2017-09-08 18:18:10 +03:00
// Adding a remote should not go in the remotes.d dir unless this is a
// system repo or add-remotes-config-dir is set to true
2017-09-12 20:23:31 +03:00
repo . remote _add ( 'bar' , 'http://bar' , remoteOptions , null ) ;
2017-09-07 22:02:51 +03:00
remotes = repo . remote _list ( )
assertNotEquals ( remotes . indexOf ( 'bar' ) , - 1 ) ;
assertEquals ( remotesDir . get _child ( 'bar.conf' ) . query _exists ( null ) , false ) ;
print ( "ok add-not-in-remotes-config-dir" ) ;
// Removing the remotes.d remote should delete the conf file
repo . remote _delete ( 'foo' , null ) ;
remotes = repo . remote _list ( )
assertEquals ( remotes . indexOf ( 'foo' ) , - 1 ) ;
assertEquals ( remotesDir . get _child ( 'foo.conf' ) . query _exists ( null ) , false ) ;
print ( "ok delete-in-remotes-config-dir" ) ;
2017-09-08 18:18:10 +03:00
// Set add-remotes-config-dir to true and check that a remote gets added
// in the config dir
let repoConfig = repo . copy _config ( ) ;
repoConfig . set _boolean ( 'core' , 'add-remotes-config-dir' , true ) ;
repo . write _config ( repoConfig ) ;
repo . reload _config ( null ) ;
2017-09-12 20:23:31 +03:00
repo . remote _add ( 'baz' , 'http://baz' , remoteOptions , null ) ;
2017-09-08 18:18:10 +03:00
remotes = repo . remote _list ( )
assertNotEquals ( remotes . indexOf ( 'baz' ) , - 1 ) ;
assertEquals ( remotesDir . get _child ( 'baz.conf' ) . query _exists ( null ) , true ) ;
print ( "ok add-in-remotes-config-dir" ) ;
2017-09-11 20:57:42 +03:00
// Trying to set a remote config option via write_config() for a remote
// defined in the config file should succeed
2019-08-30 00:54:17 +03:00
try {
let [ , gpg _verify ] = repo . remote _get _gpg _verify ( 'bar' ) ;
assertEquals ( gpg _verify , true ) ;
repoConfig = repo . copy _config ( ) ;
repoConfig . set _boolean ( 'remote "bar"' , 'gpg-verify' , false ) ;
repo . write _config ( repoConfig ) ;
repo . reload _config ( null ) ;
[ , gpg _verify ] = repo . remote _get _gpg _verify ( 'bar' ) ;
assertEquals ( gpg _verify , false ) ;
print ( "ok config-remote-in-config-file-succeeds" ) ;
} catch ( e ) {
// Skip this test if GPG is not supported
if ( ! ( e . matches ( Gio . IOErrorEnum , Gio . IOErrorEnum . NOT _SUPPORTED ) ) )
throw e ;
print ( "ok config-remote-in-config-file-succeeds # SKIP due build without GPG support" ) ;
}
2017-09-11 20:57:42 +03:00
// Trying to set a remote config option via write_config() for a remote
// defined in the config dir should fail with G_IO_ERROR_EXISTS
repoConfig = repo . copy _config ( ) ;
repoConfig . set _boolean ( 'remote "baz"' , 'gpg-verify' , false ) ;
try {
if ( repo . write _config ( repoConfig ) )
throw new Error ( "config of remote in config dir should fail" ) ;
} catch ( e ) {
if ( ! ( e . matches ( Gio . IOErrorEnum , Gio . IOErrorEnum . EXISTS ) ) )
throw e ;
}
print ( "ok config-remote-in-config-dir-fails" ) ;
2017-09-12 20:23:31 +03:00
// Replacing a non-existent remote should succeed. This should go in the
// config dir since add-remote-config-dir is set to true above
repo . remote _change ( null , OSTree . RepoRemoteChange . REPLACE ,
'nonexistent' , 'http://nonexistent' ,
null , null ) ;
remotes = repo . remote _list ( ) ;
assertNotEquals ( remotes . indexOf ( 'nonexistent' ) , - 1 ) ;
assertEquals ( remotesDir . get _child ( 'nonexistent.conf' ) . query _exists ( null ) , true ) ;
print ( "ok replace-missing-remote-succeeds" ) ;
// Test replacing remote options in config dir. This should remove the
// branches setting above
repo . remote _change ( null , OSTree . RepoRemoteChange . REPLACE , 'baz' ,
'http://baz2' , null , null ) ;
remoteConfigFile = remotesDir . get _child ( 'baz.conf' ) ;
remoteConfig = GLib . KeyFile . new ( ) ;
remoteConfig . load _from _file ( remoteConfigFile . get _path ( ) ,
GLib . KeyFileFlags . NONE ) ;
assertEquals ( remoteConfig . get _value ( 'remote "baz"' , 'url' ) , 'http://baz2' ) ;
try {
remoteConfig . get _string _list ( 'remote "baz"' , 'branches' ) ;
throw new Error ( 'baz remote should not have branches option' ) ;
} catch ( e ) {
if ( ! ( e . matches ( GLib . KeyFileError , GLib . KeyFileError . KEY _NOT _FOUND ) ) )
throw e ;
}
print ( "ok replace-remote-in-config-dir" ) ;
// Test replacing remote options in config file. This should remove the
// branches setting above
repo . remote _change ( null , OSTree . RepoRemoteChange . REPLACE , 'bar' ,
'http://bar2' , null , null ) ;
repoConfig = repo . get _config ( ) ;
assertEquals ( repoConfig . get _value ( 'remote "bar"' , 'url' ) , 'http://bar2' ) ;
try {
repoConfig . get _string _list ( 'remote "bar"' , 'branches' ) ;
throw new Error ( 'bar remote should not have branches option' ) ;
} catch ( e ) {
if ( ! ( e . matches ( GLib . KeyFileError , GLib . KeyFileError . KEY _NOT _FOUND ) ) )
throw e ;
}
print ( "ok replace-remote-in-config-file" ) ;