1// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package objabi
6
7import (
8	"path/filepath"
9	"runtime"
10	"testing"
11)
12
13// On Windows, "/foo" is reported as a relative path
14// (it is relative to the current drive letter),
15// so we need add a drive letter to test absolute path cases.
16func drive() string {
17	if runtime.GOOS == "windows" {
18		return "c:"
19	}
20	return ""
21}
22
23var absFileTests = []struct {
24	dir      string
25	file     string
26	rewrites string
27	abs      string
28}{
29	{"/d", "f", "", "/d/f"},
30	{"/d", drive() + "/f", "", drive() + "/f"},
31	{"/d", "f/g", "", "/d/f/g"},
32	{"/d", drive() + "/f/g", "", drive() + "/f/g"},
33
34	{"/d", "f", "/d/f", "??"},
35	{"/d", "f/g", "/d/f", "g"},
36	{"/d", "f/g", "/d/f=>h", "h/g"},
37	{"/d", "f/g", "/d/f=>/h", "/h/g"},
38	{"/d", "f/g", "/d/f=>/h;/d/e=>/i", "/h/g"},
39	{"/d", "e/f", "/d/f=>/h;/d/e=>/i", "/i/f"},
40}
41
42func TestAbsFile(t *testing.T) {
43	for _, tt := range absFileTests {
44		abs := filepath.FromSlash(AbsFile(filepath.FromSlash(tt.dir), filepath.FromSlash(tt.file), tt.rewrites))
45		want := filepath.FromSlash(tt.abs)
46		if abs != want {
47			t.Errorf("AbsFile(%q, %q, %q) = %q, want %q", tt.dir, tt.file, tt.rewrites, abs, want)
48		}
49	}
50}
51