LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package os_test | 5 package os_test |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "fmt" | 9 "fmt" |
10 "io" | 10 "io" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 file, err := Open(name) | 70 file, err := Open(name) |
71 defer file.Close() | 71 defer file.Close() |
72 if err != nil { | 72 if err != nil { |
73 t.Fatal("open failed:", err) | 73 t.Fatal("open failed:", err) |
74 } | 74 } |
75 var buf [100]byte | 75 var buf [100]byte |
76 len := 0 | 76 len := 0 |
77 for { | 77 for { |
78 n, e := file.Read(buf[0:]) | 78 n, e := file.Read(buf[0:]) |
79 len += n | 79 len += n |
80 » » if e == EOF { | 80 » » if e == io.EOF { |
81 break | 81 break |
82 } | 82 } |
83 if e != nil { | 83 if e != nil { |
84 t.Fatal("read failed:", err) | 84 t.Fatal("read failed:", err) |
85 } | 85 } |
86 } | 86 } |
87 return int64(len) | 87 return int64(len) |
88 } | 88 } |
89 | 89 |
90 func equal(name1, name2 string) (r bool) { | 90 func equal(name1, name2 string) (r bool) { |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 testReaddir(".", dot, t) | 250 testReaddir(".", dot, t) |
251 testReaddir(sysdir.name, sysdir.files, t) | 251 testReaddir(sysdir.name, sysdir.files, t) |
252 } | 252 } |
253 | 253 |
254 // Read the directory one entry at a time. | 254 // Read the directory one entry at a time. |
255 func smallReaddirnames(file *File, length int, t *testing.T) []string { | 255 func smallReaddirnames(file *File, length int, t *testing.T) []string { |
256 names := make([]string, length) | 256 names := make([]string, length) |
257 count := 0 | 257 count := 0 |
258 for { | 258 for { |
259 d, err := file.Readdirnames(1) | 259 d, err := file.Readdirnames(1) |
260 » » if err == EOF { | 260 » » if err == io.EOF { |
261 break | 261 break |
262 } | 262 } |
263 if err != nil { | 263 if err != nil { |
264 t.Fatalf("readdirnames %q failed: %v", file.Name(), err) | 264 t.Fatalf("readdirnames %q failed: %v", file.Name(), err) |
265 } | 265 } |
266 if len(d) == 0 { | 266 if len(d) == 0 { |
267 t.Fatalf("readdirnames %q returned empty slice and no er
ror", file.Name()) | 267 t.Fatalf("readdirnames %q returned empty slice and no er
ror", file.Name()) |
268 } | 268 } |
269 names[count] = d[0] | 269 names[count] = d[0] |
270 count++ | 270 count++ |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 f, err := Create(filepath.Join(dir, fmt.Sprintf("%d", i))) | 321 f, err := Create(filepath.Join(dir, fmt.Sprintf("%d", i))) |
322 if err != nil { | 322 if err != nil { |
323 t.Fatalf("Create: %v", err) | 323 t.Fatalf("Create: %v", err) |
324 } | 324 } |
325 f.Write([]byte(strings.Repeat("X", i))) | 325 f.Write([]byte(strings.Repeat("X", i))) |
326 f.Close() | 326 f.Close() |
327 } | 327 } |
328 | 328 |
329 var d *File | 329 var d *File |
330 openDir := func() { | 330 openDir := func() { |
331 » » var err Error | 331 » » var err error |
332 d, err = Open(dir) | 332 d, err = Open(dir) |
333 if err != nil { | 333 if err != nil { |
334 t.Fatalf("Open directory: %v", err) | 334 t.Fatalf("Open directory: %v", err) |
335 } | 335 } |
336 } | 336 } |
337 | 337 |
338 » readDirExpect := func(n, want int, wantErr Error) { | 338 » readDirExpect := func(n, want int, wantErr error) { |
339 fi, err := d.Readdir(n) | 339 fi, err := d.Readdir(n) |
340 if err != wantErr { | 340 if err != wantErr { |
341 t.Fatalf("Readdir of %d got error %v, want %v", n, err,
wantErr) | 341 t.Fatalf("Readdir of %d got error %v, want %v", n, err,
wantErr) |
342 } | 342 } |
343 if g, e := len(fi), want; g != e { | 343 if g, e := len(fi), want; g != e { |
344 t.Errorf("Readdir of %d got %d files, want %d", n, g, e) | 344 t.Errorf("Readdir of %d got %d files, want %d", n, g, e) |
345 } | 345 } |
346 } | 346 } |
347 | 347 |
348 » readDirNamesExpect := func(n, want int, wantErr Error) { | 348 » readDirNamesExpect := func(n, want int, wantErr error) { |
349 fi, err := d.Readdirnames(n) | 349 fi, err := d.Readdirnames(n) |
350 if err != wantErr { | 350 if err != wantErr { |
351 t.Fatalf("Readdirnames of %d got error %v, want %v", n,
err, wantErr) | 351 t.Fatalf("Readdirnames of %d got error %v, want %v", n,
err, wantErr) |
352 } | 352 } |
353 if g, e := len(fi), want; g != e { | 353 if g, e := len(fi), want; g != e { |
354 t.Errorf("Readdirnames of %d got %d files, want %d", n,
g, e) | 354 t.Errorf("Readdirnames of %d got %d files, want %d", n,
g, e) |
355 } | 355 } |
356 } | 356 } |
357 | 357 |
358 » for _, fn := range []func(int, int, Error){readDirExpect, readDirNamesEx
pect} { | 358 » for _, fn := range []func(int, int, error){readDirExpect, readDirNamesEx
pect} { |
359 // Test the slurp case | 359 // Test the slurp case |
360 openDir() | 360 openDir() |
361 fn(0, 105, nil) | 361 fn(0, 105, nil) |
362 fn(0, 0, nil) | 362 fn(0, 0, nil) |
363 d.Close() | 363 d.Close() |
364 | 364 |
365 // Slurp with -1 instead | 365 // Slurp with -1 instead |
366 openDir() | 366 openDir() |
367 fn(-1, 105, nil) | 367 fn(-1, 105, nil) |
368 fn(-2, 0, nil) | 368 fn(-2, 0, nil) |
369 fn(0, 0, nil) | 369 fn(0, 0, nil) |
370 d.Close() | 370 d.Close() |
371 | 371 |
372 // Test the bounded case | 372 // Test the bounded case |
373 openDir() | 373 openDir() |
374 fn(1, 1, nil) | 374 fn(1, 1, nil) |
375 fn(2, 2, nil) | 375 fn(2, 2, nil) |
376 fn(105, 102, nil) // and tests buffer >100 case | 376 fn(105, 102, nil) // and tests buffer >100 case |
377 » » fn(3, 0, EOF) | 377 » » fn(3, 0, io.EOF) |
378 d.Close() | 378 d.Close() |
379 } | 379 } |
380 } | 380 } |
381 | 381 |
382 func TestHardLink(t *testing.T) { | 382 func TestHardLink(t *testing.T) { |
383 // Hardlinks are not supported under windows or Plan 9. | 383 // Hardlinks are not supported under windows or Plan 9. |
384 if syscall.OS == "windows" || syscall.OS == "plan9" { | 384 if syscall.OS == "windows" || syscall.OS == "plan9" { |
385 return | 385 return |
386 } | 386 } |
387 from, to := "hardlinktestfrom", "hardlinktestto" | 387 from, to := "hardlinktestfrom", "hardlinktestto" |
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
834 {5, 0, 5}, | 834 {5, 0, 5}, |
835 {0, 2, int64(len(data))}, | 835 {0, 2, int64(len(data))}, |
836 {0, 0, 0}, | 836 {0, 0, 0}, |
837 {-1, 2, int64(len(data)) - 1}, | 837 {-1, 2, int64(len(data)) - 1}, |
838 {1 << 33, 0, 1 << 33}, | 838 {1 << 33, 0, 1 << 33}, |
839 {1 << 33, 2, 1<<33 + int64(len(data))}, | 839 {1 << 33, 2, 1<<33 + int64(len(data))}, |
840 } | 840 } |
841 for i, tt := range tests { | 841 for i, tt := range tests { |
842 off, err := f.Seek(tt.in, tt.whence) | 842 off, err := f.Seek(tt.in, tt.whence) |
843 if off != tt.out || err != nil { | 843 if off != tt.out || err != nil { |
844 » » » if e, ok := err.(*PathError); ok && e.Error == EINVAL &&
tt.out > 1<<32 { | 844 » » » if e, ok := err.(*PathError); ok && e.Err == EINVAL && t
t.out > 1<<32 { |
845 // Reiserfs rejects the big seeks. | 845 // Reiserfs rejects the big seeks. |
846 // http://code.google.com/p/go/issues/detail?id=
91 | 846 // http://code.google.com/p/go/issues/detail?id=
91 |
847 break | 847 break |
848 } | 848 } |
849 t.Errorf("#%d: Seek(%v, %v) = %v, %v want %v, nil", i, t
t.in, tt.whence, off, err, tt.out) | 849 t.Errorf("#%d: Seek(%v, %v) = %v, %v want %v, nil", i, t
t.in, tt.whence, off, err, tt.out) |
850 } | 850 } |
851 } | 851 } |
852 } | 852 } |
853 | 853 |
854 type openErrorTest struct { | 854 type openErrorTest struct { |
855 path string | 855 path string |
856 mode int | 856 mode int |
857 » error Error | 857 » error error |
858 } | 858 } |
859 | 859 |
860 var openErrorTests = []openErrorTest{ | 860 var openErrorTests = []openErrorTest{ |
861 { | 861 { |
862 sfdir + "/no-such-file", | 862 sfdir + "/no-such-file", |
863 O_RDONLY, | 863 O_RDONLY, |
864 ENOENT, | 864 ENOENT, |
865 }, | 865 }, |
866 { | 866 { |
867 sfdir, | 867 sfdir, |
(...skipping 12 matching lines...) Expand all Loading... |
880 f, err := OpenFile(tt.path, tt.mode, 0) | 880 f, err := OpenFile(tt.path, tt.mode, 0) |
881 if err == nil { | 881 if err == nil { |
882 t.Errorf("Open(%q, %d) succeeded", tt.path, tt.mode) | 882 t.Errorf("Open(%q, %d) succeeded", tt.path, tt.mode) |
883 f.Close() | 883 f.Close() |
884 continue | 884 continue |
885 } | 885 } |
886 perr, ok := err.(*PathError) | 886 perr, ok := err.(*PathError) |
887 if !ok { | 887 if !ok { |
888 t.Errorf("Open(%q, %d) returns error of %T type; want *o
s.PathError", tt.path, tt.mode, err) | 888 t.Errorf("Open(%q, %d) returns error of %T type; want *o
s.PathError", tt.path, tt.mode, err) |
889 } | 889 } |
890 » » if perr.Error != tt.error { | 890 » » if perr.Err != tt.error { |
891 if syscall.OS == "plan9" { | 891 if syscall.OS == "plan9" { |
892 » » » » syscallErrStr := perr.Error.String() | 892 » » » » syscallErrStr := perr.Err.Error() |
893 » » » » expectedErrStr := strings.Replace(tt.error.Strin
g(), "file ", "", 1) | 893 » » » » expectedErrStr := strings.Replace(tt.error.Error
(), "file ", "", 1) |
894 if !strings.HasSuffix(syscallErrStr, expectedErr
Str) { | 894 if !strings.HasSuffix(syscallErrStr, expectedErr
Str) { |
895 t.Errorf("Open(%q, %d) = _, %q; want suf
fix %q", tt.path, tt.mode, syscallErrStr, expectedErrStr) | 895 t.Errorf("Open(%q, %d) = _, %q; want suf
fix %q", tt.path, tt.mode, syscallErrStr, expectedErrStr) |
896 } | 896 } |
897 } else { | 897 } else { |
898 » » » » t.Errorf("Open(%q, %d) = _, %q; want %q", tt.pat
h, tt.mode, perr.Error.String(), tt.error.String()) | 898 » » » » t.Errorf("Open(%q, %d) = _, %q; want %q", tt.pat
h, tt.mode, perr.Err.Error(), tt.error.Error()) |
899 } | 899 } |
900 } | 900 } |
901 } | 901 } |
902 } | 902 } |
903 | 903 |
904 func run(t *testing.T, cmd []string) string { | 904 func run(t *testing.T, cmd []string) string { |
905 // Run /bin/hostname and collect output. | 905 // Run /bin/hostname and collect output. |
906 r, w, err := Pipe() | 906 r, w, err := Pipe() |
907 if err != nil { | 907 if err != nil { |
908 t.Fatal(err) | 908 t.Fatal(err) |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1076 } | 1076 } |
1077 } | 1077 } |
1078 | 1078 |
1079 func TestNilWaitmsgString(t *testing.T) { | 1079 func TestNilWaitmsgString(t *testing.T) { |
1080 var w *Waitmsg | 1080 var w *Waitmsg |
1081 s := w.String() | 1081 s := w.String() |
1082 if s != "<nil>" { | 1082 if s != "<nil>" { |
1083 t.Errorf("(*Waitmsg)(nil).String() = %q, want %q", s, "<nil>") | 1083 t.Errorf("(*Waitmsg)(nil).String() = %q, want %q", s, "<nil>") |
1084 } | 1084 } |
1085 } | 1085 } |
LEFT | RIGHT |