Home | History | Annotate | Download | only in os
      1 // Copyright 2016 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 
      5 package os_test
      6 
      7 import (
      8 	"os"
      9 	"strings"
     10 	"testing"
     11 )
     12 
     13 func TestFixLongPath(t *testing.T) {
     14 	// 248 is long enough to trigger the longer-than-248 checks in
     15 	// fixLongPath, but short enough not to make a path component
     16 	// longer than 255, which is illegal on Windows. (which
     17 	// doesn't really matter anyway, since this is purely a string
     18 	// function we're testing, and it's not actually being used to
     19 	// do a system call)
     20 	veryLong := "l" + strings.Repeat("o", 248) + "ng"
     21 	for _, test := range []struct{ in, want string }{
     22 		// Short; unchanged:
     23 		{`C:\short.txt`, `C:\short.txt`},
     24 		{`C:\`, `C:\`},
     25 		{`C:`, `C:`},
     26 		// The "long" substring is replaced by a looooooong
     27 		// string which triggers the rewriting. Except in the
     28 		// cases below where it doesn't.
     29 		{`C:\long\foo.txt`, `\\?\C:\long\foo.txt`},
     30 		{`C:/long/foo.txt`, `\\?\C:\long\foo.txt`},
     31 		{`C:\long\foo\\bar\.\baz\\`, `\\?\C:\long\foo\bar\baz`},
     32 		{`\\unc\path`, `\\unc\path`},
     33 		{`long.txt`, `long.txt`},
     34 		{`C:long.txt`, `C:long.txt`},
     35 		{`c:\long\..\bar\baz`, `c:\long\..\bar\baz`},
     36 		{`\\?\c:\long\foo.txt`, `\\?\c:\long\foo.txt`},
     37 		{`\\?\c:\long/foo.txt`, `\\?\c:\long/foo.txt`},
     38 	} {
     39 		in := strings.Replace(test.in, "long", veryLong, -1)
     40 		want := strings.Replace(test.want, "long", veryLong, -1)
     41 		if got := os.FixLongPath(in); got != want {
     42 			got = strings.Replace(got, veryLong, "long", -1)
     43 			t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want)
     44 		}
     45 	}
     46 }
     47