package app

import (
	"os"
	"testing"
)

func TestParse(t *testing.T) {
	tests := []struct {
		src         string
		dst         string
		srcPath     string
		dstPath     string
		service     string
		toContainer bool
		err         error
	}{
		{src: "foo", dst: "bar", err: errServiceMissing},
		{src: "app:foo", dst: "app:bar", err: errServiceMissing},
		{src: "app:foo", dst: "bar", srcPath: "foo", dstPath: "bar", service: "app", toContainer: false},
		{src: "foo", dst: "app:bar", srcPath: "foo", dstPath: "bar", service: "app", toContainer: true},
	}

	for i, tc := range tests {
		srcPath, dstPath, service, toContainer, err := parseSrcAndDst(tc.src, tc.dst)
		if srcPath != tc.srcPath {
			t.Errorf("[%d] srcPath: want (%s), got(%s)", i, tc.srcPath, srcPath)
		}
		if dstPath != tc.dstPath {
			t.Errorf("[%d] dstPath: want (%s), got(%s)", i, tc.dstPath, dstPath)
		}
		if service != tc.service {
			t.Errorf("[%d] service: want (%s), got(%s)", i, tc.service, service)
		}
		if toContainer != tc.toContainer {
			t.Errorf("[%d] toConainer: want (%t), got(%t)", i, tc.toContainer, toContainer)
		}
		if err == nil && tc.err != nil && err.Error() != tc.err.Error() {
			t.Errorf("[%d] err: want (%s), got(%s)", i, tc.err, err)
		}
	}
}

func TestCopyMode(t *testing.T) {
	tests := []struct {
		srcPath   string
		dstPath   string
		srcMode   os.FileMode
		dstMode   os.FileMode
		dstExists bool
		mode      CopyMode
		err       error
	}{
		{
			srcPath:   "foo.txt",
			dstPath:   "foo.txt",
			srcMode:   os.ModePerm,
			dstMode:   os.ModePerm,
			dstExists: true,
			mode:      CopyModeFileToFile,
		},
		{
			srcPath:   "foo.txt",
			dstPath:   "bar.txt",
			srcMode:   os.ModePerm,
			dstExists: true,
			mode:      CopyModeFileToFileRename,
		},
		{
			srcPath:   "foo",
			dstPath:   "foo",
			srcMode:   os.ModeDir,
			dstMode:   os.ModeDir,
			dstExists: true,
			mode:      CopyModeDirToDir,
		},
		{
			srcPath:   "foo/",
			dstPath:   "foo",
			srcMode:   os.ModeDir,
			dstMode:   os.ModeDir,
			dstExists: true,
			mode:      CopyModeFilesToDir,
		},
		{
			srcPath:   "foo",
			dstPath:   "foo",
			srcMode:   os.ModeDir,
			dstExists: false,
			mode:      -1,
			err:       ErrDstDirNotExist,
		},
		{
			srcPath:   "foo",
			dstPath:   "foo",
			srcMode:   os.ModeDir,
			dstMode:   os.ModePerm,
			dstExists: true,
			mode:      -1,
			err:       ErrCopyDirToFile,
		},
	}

	for i, tc := range tests {
		mode, err := copyMode(tc.srcPath, tc.dstPath, tc.srcMode, tc.dstMode, tc.dstExists)
		if mode != tc.mode {
			t.Errorf("[%d] mode: want (%d), got(%d)", i, tc.mode, mode)
		}
		if err != tc.err {
			t.Errorf("[%d] err: want (%s), got(%s)", i, tc.err, err)
		}
	}
}