<commit_before>#!/usr/bin/env bash
. wvtest.sh
. t/lib.sh

TOP="$(pwd)"
export BUP_DIR="$TOP/buptest.tmp"

bup()
{
    "$TOP/bup" "$@"
}

hardlink-sets()
{
    "$TOP/t/hardlink-sets" "$@"
}

# Very simple metadata tests -- create a test tree then check that bup
# meta can reproduce the metadata correctly (according to bup xstat)
# via create, extract, start-extract, and finish-extract.  The current
# tests are crude, and this does not fully test devices, varying
# users/groups, acls, attrs, etc.

genstat()
{
    (
        export PATH="$TOP:$PATH" # pick up bup
        # Skip atime (test elsewhere) to avoid the observer effect.
        find . | sort | xargs bup xstat --exclude-fields ctime,atime,size
    )
}

test-src-create-extract()
{
    # Test bup meta create/extract for ./src -> ./src-restore.
    # Also writes to ./src-stat and ./src-restore-stat.
    (
        (cd src && WVPASS genstat) > src-stat
        WVPASS bup meta --create --recurse --file src.meta src
        # Test extract.
        force-delete src-restore
        mkdir src-restore
        cd src-restore
        WVPASS bup meta --extract --file ../src.meta
        WVPASS test -d src
        (cd src && genstat >../../src-restore-stat) || WVFAIL
        WVPASS diff -U5 ../src-stat ../src-restore-stat
        # Test start/finish extract.
        force-delete src
        WVPASS bup meta --start-extract --file ../src.meta
        WVPASS test -d src
        WVPASS bup meta --finish-extract --file ../src.meta
        (cd src && genstat >../../src-restore-stat) || WVFAIL
        WVPASS diff -U5 ../src-stat ../src-restore-stat
    )
}

test-src-save-restore()
{
    # Test bup save/restore metadata for ./src -> ./src-restore.  Also
    # writes to ./src.bup.  Note that for now this just tests the
    # restore below src/, in order to avoid having to worry about
    # operations that require root (like chown /home).
    (
        set -x
        rm -rf src.bup
        mkdir src.bup
        export BUP_DIR=$(pwd)/src.bup
        WVPASS bup init
        WVPASS bup index src
        WVPASS bup save -t -n src src
        # Test extract.
        force-delete src-restore
        mkdir src-restore
        WVPASS bup restore -C src-restore "/src/latest$(pwd)/"
        WVPASS test -d src-restore/src
        WVPASS "$TOP/t/compare-trees" -c src/ src-restore/src/
        rm -rf src.bup
        set +x
    )
}

universal-cleanup()
{
    if [ $(t/root-status) != root ]; then return 0; fi
    cd "$TOP"
    umount "$TOP/bupmeta.tmp/testfs" || true
    umount "$TOP/bupmeta.tmp/testfs-limited" || true
}

universal-cleanup
trap universal-cleanup EXIT

setup-test-tree()
(
    set -e
    force-delete "$BUP_DIR"
    force-delete "$TOP/bupmeta.tmp"
    mkdir -p "$TOP/bupmeta.tmp/src"
    cp -pPR Documentation cmd lib t "$TOP/bupmeta.tmp"/src

    # Add some hard links for the general tests.
    (
        cd "$TOP/bupmeta.tmp"/src
        touch hardlink-target
        ln hardlink-target hardlink-1
        ln hardlink-target hardlink-2
        ln hardlink-target hardlink-3
    )

    # Add some trivial files for the index, modify, save tests.
    (
        cd "$TOP/bupmeta.tmp"/src
        mkdir volatile
        touch volatile/{1,2,3}
    )

    # Regression test for metadata sort order.  Previously, these two
    # entries would sort in the wrong order because the metadata
    # entries were being sorted by mangled name, but the index isn't.
    dd if=/dev/zero of="$TOP/bupmeta.tmp"/src/foo bs=1k count=33
    touch -d 2011-11-11 "$TOP/bupmeta.tmp"/src/foo
    touch -d 2011-12-12 "$TOP/bupmeta.tmp"/src/foo-bar

    t/mksock "$TOP/bupmeta.tmp/src/test-socket" || true
) || WVFAIL

# Use the test tree to check bup meta.<commit_msg>test-meta.sh: use the more portable "touch -t" instead of "touch -d".<commit_after>#!/usr/bin/env bash
. wvtest.sh
. t/lib.sh

TOP="$(pwd)"
export BUP_DIR="$TOP/buptest.tmp"

bup()
{
    "$TOP/bup" "$@"
}

hardlink-sets()
{
    "$TOP/t/hardlink-sets" "$@"
}

# Very simple metadata tests -- create a test tree then check that bup
# meta can reproduce the metadata correctly (according to bup xstat)
# via create, extract, start-extract, and finish-extract.  The current
# tests are crude, and this does not fully test devices, varying
# users/groups, acls, attrs, etc.

genstat()
{
    (
        export PATH="$TOP:$PATH" # pick up bup
        # Skip atime (test elsewhere) to avoid the observer effect.
        find . | sort | xargs bup xstat --exclude-fields ctime,atime,size
    )
}

test-src-create-extract()
{
    # Test bup meta create/extract for ./src -> ./src-restore.
    # Also writes to ./src-stat and ./src-restore-stat.
    (
        (cd src && WVPASS genstat) > src-stat
        WVPASS bup meta --create --recurse --file src.meta src
        # Test extract.
        force-delete src-restore
        mkdir src-restore
        cd src-restore
        WVPASS bup meta --extract --file ../src.meta
        WVPASS test -d src
        (cd src && genstat >../../src-restore-stat) || WVFAIL
        WVPASS diff -U5 ../src-stat ../src-restore-stat
        # Test start/finish extract.
        force-delete src
        WVPASS bup meta --start-extract --file ../src.meta
        WVPASS test -d src
        WVPASS bup meta --finish-extract --file ../src.meta
        (cd src && genstat >../../src-restore-stat) || WVFAIL
        WVPASS diff -U5 ../src-stat ../src-restore-stat
    )
}

test-src-save-restore()
{
    # Test bup save/restore metadata for ./src -> ./src-restore.  Also
    # writes to ./src.bup.  Note that for now this just tests the
    # restore below src/, in order to avoid having to worry about
    # operations that require root (like chown /home).
    (
        set -x
        rm -rf src.bup
        mkdir src.bup
        export BUP_DIR=$(pwd)/src.bup
        WVPASS bup init
        WVPASS bup index src
        WVPASS bup save -t -n src src
        # Test extract.
        force-delete src-restore
        mkdir src-restore
        WVPASS bup restore -C src-restore "/src/latest$(pwd)/"
        WVPASS test -d src-restore/src
        WVPASS "$TOP/t/compare-trees" -c src/ src-restore/src/
        rm -rf src.bup
        set +x
    )
}

universal-cleanup()
{
    if [ $(t/root-status) != root ]; then return 0; fi
    cd "$TOP"
    umount "$TOP/bupmeta.tmp/testfs" || true
    umount "$TOP/bupmeta.tmp/testfs-limited" || true
}

universal-cleanup
trap universal-cleanup EXIT

setup-test-tree()
(
    set -e
    force-delete "$BUP_DIR"
    force-delete "$TOP/bupmeta.tmp"
    mkdir -p "$TOP/bupmeta.tmp/src"
    cp -pPR Documentation cmd lib t "$TOP/bupmeta.tmp"/src

    # Add some hard links for the general tests.
    (
        cd "$TOP/bupmeta.tmp"/src
        touch hardlink-target
        ln hardlink-target hardlink-1
        ln hardlink-target hardlink-2
        ln hardlink-target hardlink-3
    )

    # Add some trivial files for the index, modify, save tests.
    (
        cd "$TOP/bupmeta.tmp"/src
        mkdir volatile
        touch volatile/{1,2,3}
    )

    # Regression test for metadata sort order.  Previously, these two
    # entries would sort in the wrong order because the metadata
    # entries were being sorted by mangled name, but the index isn't.
    dd if=/dev/zero of="$TOP/bupmeta.tmp"/src/foo bs=1k count=33
    touch -t 201111111111 "$TOP/bupmeta.tmp"/src/foo
    touch -t 201112121111 "$TOP/bupmeta.tmp"/src/foo-bar

    t/mksock "$TOP/bupmeta.tmp/src/test-socket" || true
) || WVFAIL

# Use the test tree to check bup meta.
{'fraction_non_alphanumeric': 0.12440063931806074, 'fraction_numerical': 0.008524240809802876, 'mean_word_length': 2.1080298013245033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2918621', 'n_tokens_mistral': 2589, 'n_tokens_neox': 2474, 'n_words': 769}