Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package build |
| 16 | |
Dan Willemsen | fe8b645 | 2018-05-12 18:34:24 -0700 | [diff] [blame^] | 17 | import ( |
| 18 | "io/ioutil" |
| 19 | "os" |
| 20 | "path/filepath" |
| 21 | "testing" |
| 22 | |
| 23 | "android/soong/ui/logger" |
| 24 | ) |
| 25 | |
| 26 | func TestEnsureEmptyDirs(t *testing.T) { |
| 27 | ctx := testContext() |
| 28 | defer logger.Recover(func(err error) { |
| 29 | t.Error(err) |
| 30 | }) |
| 31 | |
| 32 | tmpDir, err := ioutil.TempDir("", "") |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | defer func() { |
| 37 | err := os.RemoveAll(tmpDir) |
| 38 | if err != nil { |
| 39 | t.Errorf("Error removing tmpDir: %v", err) |
| 40 | } |
| 41 | }() |
| 42 | |
| 43 | ensureEmptyDirectoriesExist(ctx, filepath.Join(tmpDir, "a/b")) |
| 44 | |
| 45 | err = os.Chmod(filepath.Join(tmpDir, "a"), 0555) |
| 46 | if err != nil { |
| 47 | t.Fatalf("Failed to chown: %v", err) |
| 48 | } |
| 49 | |
| 50 | ensureEmptyDirectoriesExist(ctx, filepath.Join(tmpDir, "a")) |
| 51 | } |
Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 52 | |
| 53 | func TestStripAnsiEscapes(t *testing.T) { |
| 54 | testcases := []struct { |
| 55 | input string |
| 56 | output string |
| 57 | }{ |
| 58 | { |
| 59 | "", |
| 60 | "", |
| 61 | }, |
| 62 | { |
| 63 | "This is a test", |
| 64 | "This is a test", |
| 65 | }, |
| 66 | { |
| 67 | "interrupted: \x1b[12", |
| 68 | "interrupted: ", |
| 69 | }, |
| 70 | { |
| 71 | "other \x1bescape \x1b", |
| 72 | "other \x1bescape \x1b", |
| 73 | }, |
| 74 | { // from pretty-error macro |
| 75 | "\x1b[1mart/Android.mk: \x1b[31merror:\x1b[0m\x1b[1m art: test error \x1b[0m", |
| 76 | "art/Android.mk: error: art: test error ", |
| 77 | }, |
| 78 | { // from envsetup.sh make wrapper |
| 79 | "\x1b[0;31m#### make failed to build some targets (2 seconds) ####\x1b[00m", |
| 80 | "#### make failed to build some targets (2 seconds) ####", |
| 81 | }, |
| 82 | { // from clang (via ninja testcase) |
| 83 | "\x1b[1maffixmgr.cxx:286:15: \x1b[0m\x1b[0;1;35mwarning: \x1b[0m\x1b[1musing the result... [-Wparentheses]\x1b[0m", |
| 84 | "affixmgr.cxx:286:15: warning: using the result... [-Wparentheses]", |
| 85 | }, |
| 86 | } |
| 87 | for _, tc := range testcases { |
| 88 | got := string(stripAnsiEscapes([]byte(tc.input))) |
| 89 | if got != tc.output { |
| 90 | t.Errorf("output strings didn't match\n"+ |
| 91 | "input: %#v\n"+ |
| 92 | " want: %#v\n"+ |
| 93 | " got: %#v", tc.input, tc.output, got) |
| 94 | } |
| 95 | } |
| 96 | } |