github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/i18n/i18n_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2015 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package i18n
    21  
    22  import (
    23  	"io/ioutil"
    24  	"os"
    25  	"os/exec"
    26  	"path/filepath"
    27  	"testing"
    28  
    29  	. "gopkg.in/check.v1"
    30  
    31  	"github.com/snapcore/snapd/dirs"
    32  )
    33  
    34  // Hook up check.v1 into the "go test" runner
    35  func Test(t *testing.T) { TestingT(t) }
    36  
    37  var mockLocalePo = []byte(`
    38  msgid ""
    39  msgstr ""
    40  "Project-Id-Version: snappy-test\n"
    41  "Report-Msgid-Bugs-To: snappy-devel@lists.ubuntu.com\n"
    42  "POT-Creation-Date: 2015-06-16 09:08+0200\n"
    43  "Language: en_DK\n"
    44  "MIME-Version: 1.0\n"
    45  "Content-Type: text/plain; charset=UTF-8\n"
    46  "Content-Transfer-Encoding: 8bit\n"
    47  "Plural-Forms: nplurals=2; plural=n != 1;>\n"
    48  
    49  msgid "plural_1"
    50  msgid_plural "plural_2"
    51  msgstr[0] "translated plural_1"
    52  msgstr[1] "translated plural_2"
    53  
    54  msgid "singular"
    55  msgstr "translated singular"
    56  `)
    57  
    58  func makeMockTranslations(c *C, localeDir string) {
    59  	fullLocaleDir := filepath.Join(localeDir, "en_DK", "LC_MESSAGES")
    60  	err := os.MkdirAll(fullLocaleDir, 0755)
    61  	c.Assert(err, IsNil)
    62  
    63  	po := filepath.Join(fullLocaleDir, "snappy-test.po")
    64  	mo := filepath.Join(fullLocaleDir, "snappy-test.mo")
    65  	err = ioutil.WriteFile(po, mockLocalePo, 0644)
    66  	c.Assert(err, IsNil)
    67  
    68  	cmd := exec.Command("msgfmt", po, "--output-file", mo)
    69  	cmd.Stdout = os.Stdout
    70  	cmd.Stderr = os.Stderr
    71  	err = cmd.Run()
    72  	c.Assert(err, IsNil)
    73  }
    74  
    75  type i18nTestSuite struct {
    76  	origLang       string
    77  	origLcMessages string
    78  }
    79  
    80  var _ = Suite(&i18nTestSuite{})
    81  
    82  func (s *i18nTestSuite) SetUpTest(c *C) {
    83  	// this dir contains a special hand-crafted en_DK/snappy-test.mo
    84  	// file
    85  	localeDir := c.MkDir()
    86  	makeMockTranslations(c, localeDir)
    87  
    88  	// we use a custom test mo file
    89  	TEXTDOMAIN = "snappy-test"
    90  
    91  	s.origLang = os.Getenv("LANG")
    92  	s.origLcMessages = os.Getenv("LC_MESSAGES")
    93  
    94  	bindTextDomain("snappy-test", localeDir)
    95  	os.Setenv("LANG", "en_DK.UTF-8")
    96  	setLocale("")
    97  }
    98  
    99  func (s *i18nTestSuite) TearDownTest(c *C) {
   100  	os.Setenv("LANG", s.origLang)
   101  	os.Setenv("LC_MESSAGES", s.origLcMessages)
   102  }
   103  
   104  func (s *i18nTestSuite) TestTranslatedSingular(c *C) {
   105  	// no G() to avoid adding the test string to snappy-pot
   106  	var Gtest = G
   107  	c.Assert(Gtest("singular"), Equals, "translated singular")
   108  }
   109  
   110  func (s *i18nTestSuite) TestTranslatesPlural(c *C) {
   111  	// no NG() to avoid adding the test string to snappy-pot
   112  	var NGtest = NG
   113  	c.Assert(NGtest("plural_1", "plural_2", 1), Equals, "translated plural_1")
   114  }
   115  
   116  func (s *i18nTestSuite) TestTranslatedMissingLangNoCrash(c *C) {
   117  	setLocale("invalid")
   118  
   119  	// no G() to avoid adding the test string to snappy-pot
   120  	var Gtest = G
   121  	c.Assert(Gtest("singular"), Equals, "singular")
   122  }
   123  
   124  func (s *i18nTestSuite) TestInvalidTextDomainDir(c *C) {
   125  	bindTextDomain("snappy-test", "/random/not/existing/dir")
   126  	setLocale("invalid")
   127  
   128  	// no G() to avoid adding the test string to snappy-pot
   129  	var Gtest = G
   130  	c.Assert(Gtest("singular"), Equals, "singular")
   131  }
   132  
   133  func (s *i18nTestSuite) TestLangpackResolverFromLangpack(c *C) {
   134  	root := c.MkDir()
   135  	localeDir := filepath.Join(root, "/usr/share/locale")
   136  	err := os.MkdirAll(localeDir, 0755)
   137  	c.Assert(err, IsNil)
   138  
   139  	d := filepath.Join(root, "/usr/share/locale-langpack")
   140  	makeMockTranslations(c, d)
   141  	bindTextDomain("snappy-test", localeDir)
   142  	setLocale("")
   143  
   144  	// no G() to avoid adding the test string to snappy-pot
   145  	var Gtest = G
   146  	c.Assert(Gtest("singular"), Equals, "translated singular", Commentf("test with %q failed", d))
   147  }
   148  
   149  func (s *i18nTestSuite) TestLangpackResolverFromCore(c *C) {
   150  	origSnapMountDir := dirs.SnapMountDir
   151  	defer func() { dirs.SnapMountDir = origSnapMountDir }()
   152  	dirs.SnapMountDir = c.MkDir()
   153  
   154  	d := filepath.Join(dirs.SnapMountDir, "/core/current/usr/share/locale")
   155  	makeMockTranslations(c, d)
   156  	bindTextDomain("snappy-test", "/usr/share/locale")
   157  	setLocale("")
   158  
   159  	// no G() to avoid adding the test string to snappy-pot
   160  	var Gtest = G
   161  	c.Assert(Gtest("singular"), Equals, "translated singular", Commentf("test with %q failed", d))
   162  }