2013-10-09 06:44:39 +04:00
#!/usr/bin/env gjs
//
// Copyright (C) 2013 Colin Walters <walters@verbum.org>
//
2018-01-30 22:26:26 +03:00
// SPDX-License-Identifier: LGPL-2.0+
//
2013-10-09 06:44:39 +04: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 ) ) ;
}
2017-07-20 17:32:44 +03:00
print ( '1..1' )
2013-10-09 06:44:39 +04:00
let testDataDir = Gio . File . new _for _path ( 'test-data' ) ;
testDataDir . make _directory ( null ) ;
testDataDir . get _child ( 'some-file' ) . replace _contents ( "hello world!" , null , false , 0 , null ) ;
testDataDir . get _child ( 'another-file' ) . replace _contents ( "hello world again!" , null , false , 0 , null ) ;
let repoPath = Gio . File . new _for _path ( 'repo' ) ;
let repo = OSTree . Repo . new ( repoPath ) ;
repo . create ( OSTree . RepoMode . ARCHIVE _Z2 , null ) ;
repo . open ( null ) ;
let commitModifier = OSTree . RepoCommitModifier . new ( OSTree . RepoCommitModifierFlags . GENERATE _SIZES , null ) ;
assertEquals ( repo . get _mode ( ) , OSTree . RepoMode . ARCHIVE _Z2 ) ;
repo . prepare _transaction ( null ) ;
let mtree = OSTree . MutableTree . new ( ) ;
repo . write _directory _to _mtree ( testDataDir , mtree , commitModifier , null ) ;
let [ , dirTree ] = repo . write _mtree ( mtree , null ) ;
let [ , commit ] = repo . write _commit ( null , 'Some subject' , 'Some body' , null , dirTree , null ) ;
print ( "commit => " + commit ) ;
repo . commit _transaction ( null , null ) ;
// Test the sizes metadata
let [ , commitVariant ] = repo . load _variant ( OSTree . ObjectType . COMMIT , commit ) ;
let metadata = commitVariant . get _child _value ( 0 ) ;
let sizes = metadata . lookup _value ( 'ostree.sizes' , GLib . VariantType . new ( 'aay' ) ) ;
let nSizes = sizes . n _children ( ) ;
assertEquals ( nSizes , 2 ) ;
let expectedUncompressedSizes = [ 12 , 18 ] ;
let foundExpectedUncompressedSizes = 0 ;
for ( let i = 0 ; i < nSizes ; i ++ ) {
let sizeEntry = sizes . get _child _value ( i ) . deep _unpack ( ) ;
assertEquals ( sizeEntry . length , 34 ) ;
let compressedSize = sizeEntry [ 32 ] ;
let uncompressedSize = sizeEntry [ 33 ] ;
print ( "compressed = " + compressedSize ) ;
print ( "uncompressed = " + uncompressedSize ) ;
for ( let j = 0 ; j < expectedUncompressedSizes . length ; j ++ ) {
let expected = expectedUncompressedSizes [ j ] ;
if ( expected == uncompressedSize ) {
print ( "Matched expected uncompressed size " + expected ) ;
expectedUncompressedSizes . splice ( j , 1 ) ;
break ;
}
}
}
if ( expectedUncompressedSizes . length > 0 ) {
throw new Error ( "Failed to match expectedUncompressedSizes: " + JSON . stringify ( expectedUncompressedSizes ) ) ;
}
2017-07-20 17:32:44 +03:00
print ( "ok test-sizes" ) ;