1// Copyright (C) 2013-2015 Sören Tempel 2// 3// This program is free software: you can redistribute it and/or modify 4// it under the terms of the GNU General Public License as published by 5// the Free Software Foundation, either version 3 of the License, or 6// (at your option) any later version. 7// 8// This program is distributed in the hope that it will be useful, 9// but WITHOUT ANY WARRANTY; without even the implied warranty of 10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11// GNU General Public License for more details. 12// 13// You should have received a copy of the GNU General Public License 14// along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16package util 17 18import ( 19 "io/ioutil" 20 "os" 21 "path/filepath" 22 "testing" 23) 24 25func TestLock1(t *testing.T) { 26 file, err := ioutil.TempFile(os.TempDir(), "testLock") 27 if err != nil { 28 t.Fatal(err) 29 } 30 31 defer file.Close() 32 fi, err := file.Stat() 33 if err != nil { 34 t.Fatal(err) 35 } 36 37 lockPath := filepath.Join(os.TempDir(), fi.Name()) 38 if err := Lock(lockPath); !os.IsExist(err) { 39 t.Fail() 40 } 41 42 if err := os.Remove(lockPath); err != nil { 43 t.Fatal(err) 44 } 45} 46 47func TestLock2(t *testing.T) { 48 lockPath := filepath.Join(os.TempDir(), "lockTest") 49 if err := Lock(lockPath); os.IsExist(err) { 50 t.Fatal(err) 51 } 52 53 if err := os.Remove(lockPath); err != nil { 54 t.Fatal(err) 55 } 56} 57 58func TestEscape(t *testing.T) { 59 type testpair struct { 60 unescaped string 61 escaped string 62 } 63 64 tests := []testpair{ 65 {"$$foo /", "foo"}, 66 {"Foo bar, baz!", "Foo-bar-baz"}, 67 {"LNP007: Foobar!", "LNP007-Foobar"}, 68 {"B$:(=== >$-%)/A/R", "B-A-R"}, 69 {"foobar ", "foobar"}, 70 } 71 72 for _, test := range tests { 73 e, err := Escape(test.unescaped) 74 if err != nil { 75 t.Fatal(err) 76 } 77 78 if e != test.escaped { 79 t.Fatalf("Expected %q - got %q", test.escaped, e) 80 } 81 } 82} 83 84func TestEnvDefault1(t *testing.T) { 85 if err := os.Setenv("TESTDIR", "/foo"); err != nil { 86 t.Fatal(err) 87 } 88 89 dir := EnvDefault("TESTDIR", "") 90 if dir != "/foo" { 91 t.Fatalf("Expected %q - got %q", "/foo", dir) 92 } 93} 94 95func TestEnvDefault2(t *testing.T) { 96 dir := EnvDefault("TESTDIR2", "bar") 97 if dir != filepath.Join(os.Getenv("HOME"), "bar") { 98 t.Fatalf("Expected %q - got %q", filepath.Join(os.Getenv("HOME"), "bar"), dir) 99 }100}