<commit_before>package placer

import (
	"bytes"
	"io"
	"testing"
	"time"

	. "github.com/smartystreets/goconvey/convey"

	"go.polydawn.net/rio/fs"
	"go.polydawn.net/rio/fs/osfs"
	"go.polydawn.net/rio/fsOp"
	"go.polydawn.net/rio/testutil"
)

func TestPlacers(t *testing.T) {
	Convey("Copy placer spec tests:", t, testutil.Requires(testutil.RequiresCanManageOwnership, func() {
		specPlacerGood(CopyPlacer)
	}))
	Convey("Bind placer spec tests:", t, testutil.Requires(testutil.RequiresCanMountBind, func() {
		specPlacerGood(BindPlacer)
	}))
}

func specPlacerGood(placeFunc Placer) {
	testutil.WithTmpdir(func(tmpDir fs.AbsolutePath) {
		afs := osfs.New(tmpDir)
		Convey("Placement of a dir should work, and maintain parent props", func() {
			mustPlaceFile(afs, fs.Metadata{Name: fs.MustRelPath("srcParent"), Type: fs.Type_Dir, Perms: 0755, Mtime: time.Date(2004, 01, 15, 0, 0, 0, 0, time.UTC)}, nil)
			mustPlaceFile(afs, fs.Metadata{Name: fs.MustRelPath("srcParent/content"), Type: fs.Type_Dir, Perms: 0755, Mtime: time.Date(2005, 01, 15, 0, 0, 0, 0, time.UTC)}, nil)
			mustPlaceFile(afs, fs.Metadata{Name: fs.MustRelPath("srcParent/content/file"), Type: fs.Type_File, Perms: 0640, Mtime: time.Date(2006, 01, 15, 0, 0, 0, 0, time.UTC)}, bytes.NewBuffer([]byte("asdf")))
			mustPlaceFile(afs, fs.Metadata{Name: fs.MustRelPath("dstParent"), Type: fs.Type_Dir, Perms: 0755, Mtime: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)}, nil)

			So(placeFunc(tmpDir.Join(fs.MustRelPath("srcParent/content")), tmpDir.Join(fs.MustRelPath("dstParent/content")), true), ShouldBeNil)
		})
	})
}

func mustPlaceFile(afs fs.FS, fmeta fs.Metadata, body io.Reader) {
	if fmeta.Type == fs.Type_File && body == nil {
		body = &bytes.Buffer{}
	}
	if err := fsOp.PlaceFile(afs, fmeta, body, true); err != nil {
		panic(err)
	}
}<commit_msg>stitch/placer: test with owner props.<commit_after>package placer

import (
	"bytes"
	"io"
	"testing"
	"time"

	. "github.com/smartystreets/goconvey/convey"

	"go.polydawn.net/rio/fs"
	"go.polydawn.net/rio/fs/osfs"
	"go.polydawn.net/rio/fsOp"
	"go.polydawn.net/rio/testutil"
)

func TestPlacers(t *testing.T) {
	Convey("Copy placer spec tests:", t, testutil.Requires(testutil.RequiresCanManageOwnership, func() {
		specPlacerGood(CopyPlacer)
	}))
	Convey("Bind placer spec tests:", t, testutil.Requires(testutil.RequiresCanMountBind, func() {
		specPlacerGood(BindPlacer)
	}))
}

func specPlacerGood(placeFunc Placer) {
	testutil.WithTmpdir(func(tmpDir fs.AbsolutePath) {
		afs := osfs.New(tmpDir)
		Convey("Placement of a dir should work, and maintain parent props", func() {
			mustPlaceFile(afs, fs.Metadata{Name: fs.MustRelPath("srcParent"), Type: fs.Type_Dir, Perms: 0755, Mtime: time.Date(2004, 01, 15, 0, 0, 0, 0, time.UTC)}, nil)
			mustPlaceFile(afs, fs.Metadata{Name: fs.MustRelPath("srcParent/content"), Type: fs.Type_Dir, Uid: 4000, Perms: 0755, Mtime: time.Date(2005, 01, 15, 0, 0, 0, 0, time.UTC)}, nil)
			mustPlaceFile(afs, fs.Metadata{Name: fs.MustRelPath("srcParent/content/file"), Type: fs.Type_File, Perms: 0640, Mtime: time.Date(2006, 01, 15, 0, 0, 0, 0, time.UTC)}, bytes.NewBuffer([]byte("asdf")))
			mustPlaceFile(afs, fs.Metadata{Name: fs.MustRelPath("dstParent"), Type: fs.Type_Dir, Perms: 0755, Mtime: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)}, nil)

			So(placeFunc(tmpDir.Join(fs.MustRelPath("srcParent/content")), tmpDir.Join(fs.MustRelPath("dstParent/content")), true), ShouldBeNil)
		})
	})
}

func mustPlaceFile(afs fs.FS, fmeta fs.Metadata, body io.Reader) {
	if fmeta.Type == fs.Type_File && body == nil {
		body = &bytes.Buffer{}
	}
	if err := fsOp.PlaceFile(afs, fmeta, body, false); err != nil {
		panic(err)
	}
}
{'fraction_non_alphanumeric': 0.1721980413492927, 'fraction_numerical': 0.035908596300326445, 'mean_word_length': 5.1798319327731095, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '6618817', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1453, 'n_words': 294}