Ejemplo n.º 1
0
 def test_relative_manifest_and_build_dir(self):
     content = "foo"
     output = self.from_build_dir("out")
     manifest_path = self.create_simple_echo_manifest(content, output)
     self.assertFalse(os.path.exists(output))
     ### 1st run. Generates the manifest and the output of the command.
     self.take_fs_snapshot()
     samurai = self.samurai("-C", self.tmpdir, "-p",
                            self.MANIFEST_FILENAME, self.BUILD_DIR)
     self.assertProcReturn(samurai, 0)
     self.assertFSDiff({self.build_ninja_file():FSDiffOp.CREATED,
                        self.decl_ninja_file():FSDiffOp.CREATED},
                       "ninja files not generated")
     ### 2nd run. Build outputs
     ninja = self.ninja("-C", self.build_dir())
     self.assertProcReturn(ninja, 0)
     self.assertEqual(read_file(output), content+"\n")
     self.assertFSDiff({output:FSDiffOp.CREATED}, "output file not built")
     ### 3rd run: nothing to do
     ninja = self.ninja("-C", self.build_dir())
     self.assertEqual(ninja.returncode, 0)
     self.assertFSDiff({}, "ninja modified something")
     ### 4th run: touched manifest trigger regeneration by ninja
     time.sleep(1)
     touch(manifest_path)
     self.assertFSDiff({manifest_path:FSDiffOp.UPDATED},
                       "samurai manifest not touched")
     ninja = self.ninja("-C", self.build_dir())
     self.assertProcReturn(ninja, 0)
     self.pdbg("========= ninja output")
     self.pdbg(ninja.stdout)
     self.assertFSDiff({self.build_ninja_file():FSDiffOp.UPDATED,
                        self.decl_ninja_file():FSDiffOp.UPDATED},
                       "ninja manifest not regenerated")
Ejemplo n.º 2
0
 def test_touch(self):
     path = os.path.join(self.tmpdir, "afile")
     self.assertFalse(os.path.isfile(path))
     touch(path)
     self.assertTrue(os.path.isfile(path))
     first_mtime = os.path.getmtime(path)
     time.sleep(1.0) # sleep one second because the precision is second.
     touch(path)
     second_mtime = os.path.getmtime(path)
     self.assertGreater(second_mtime, first_mtime)
Ejemplo n.º 3
0
 def test_build_cascade_targets(self):
     input1 = os.path.join(self.tmpdir, "f.in")
     output1 = self.from_build_dir("f.out1")
     output2 = self.from_build_dir("f.out2")
     fs = self.create_fs_tree({
         "f.in": "line1\n",
         self.MANIFEST_FILENAME: \
             textwrap.dedent("""\
             from samurai.sys import context as samurai_ctxt
             from samurai.test.fixture import Fixture
             Prefix = Fixture.Prefix()
             samurai_ctxt.add(Prefix(input={input1!r}, output={output1!r}))
             samurai_ctxt.add(Prefix(input={output1!r}, output={output2!r}))
             """.format(input1=input1,
                        output1=output1,
                        output2=output2)),
     })
     ### 1st run. Generates the manifest
     self.take_fs_snapshot()
     samurai = self.samurai("-p", fs[self.MANIFEST_FILENAME],
                            self.build_dir())
     self.assertProcReturn(samurai, 0)
     self.pdbg("========= build.ninja")
     self.pdbg(read_file(self.build_ninja_file()))
     self.assertFSDiff({self.build_ninja_file():FSDiffOp.CREATED,
                        self.decl_ninja_file():FSDiffOp.CREATED},
                       "ninja files not generated")
     ### 2nd run. Build outputs.
     ninja = self.ninja("-C", self.build_dir())
     self.assertProcReturn(ninja, 0)
     self.assertFSDiff({output1:FSDiffOp.CREATED,
                        output2:FSDiffOp.CREATED},
                       "output files not built")
     self.assertEqual(read_file(output1), "prefix:line1\n")
     self.assertEqual(read_file(output2), "prefix:prefix:line1\n")
     ### 3rd run: nothing to do
     ninja = self.ninja("-C", self.build_dir())
     self.assertEqual(ninja.returncode, 0)
     self.assertFSDiff({}, "ninja modified something")
     ### 4th run: touch input file triggers rebuild of two outputs.
     time.sleep(1)
     touch(input1)
     self.assertFSDiff({input1:FSDiffOp.UPDATED},
                       "input1 file not touched")
     ninja = self.ninja("-C", self.build_dir())
     self.assertEqual(ninja.returncode, 0)
     self.assertFSDiff({output1:FSDiffOp.UPDATED,
                        output2:FSDiffOp.UPDATED},
                       "output files not re-built")
Ejemplo n.º 4
0
 def test_source_is_file(self):
     source = os.path.join(samurai_sys.top_dir, "b", "c")
     touch(source, make_dirs=True)
     answer = os.path.join(samurai_sys.build_dir, "b")
     self.assertEqual(samurai_sys.get_default_build_dir_for(source), answer)
     self.assertEqual(samurai_sys.get_default_build_dir_for(PathLib(source)), PathLib(answer))
Ejemplo n.º 5
0
 def test_gen_and_regen(self):
     content = "foo"
     sub_content = "sub foo"
     output = self.from_build_dir("out")
     sub_output = self.from_build_dir("sub", "out")
     fs = self.create_fs_tree({
         self.MANIFEST_FILENAME: \
             textwrap.dedent("""\
             from samurai.sys import context as samurai_ctxt
             from samurai.subgenerator import SubSamurai
             from samurai.test.fixture import Fixture
             samurai_ctxt.add(SubSamurai("{manifest}", "{build_dir}/sub"))
             Echo = Fixture.Echo()
             samurai_ctxt.add(Echo(text={text!r}, output={output!r}))
             """.format(text=content, output=output,
                        manifest=os.path.join(self.tmpdir, "sub",
                                              self.MANIFEST_FILENAME),
                        build_dir=self.build_dir())),
         "sub": {
             self.MANIFEST_FILENAME: \
                 textwrap.dedent("""\
                 from samurai.sys import context as samurai_ctxt
                 from samurai.test.fixture import Fixture
                 Echo = Fixture.Echo()
                 samurai_ctxt.add(Echo(text={text!r}, output={output!r}))
                 """.format(text=sub_content, output=sub_output)),
         },
     })
     sub_build_ninja_file = self.from_build_dir("sub",
                                                config.BUILD_FILENAME)
     sub_decl_ninja_file = self.from_build_dir("sub",
                                               config.DECL_FILENAME)
     self.pdbg("====== manifest")
     self.pdbg(read_file(fs[self.MANIFEST_FILENAME]))
     ### 1st run: generates and build both projects.
     self.pdbg("====== #1")
     self.take_fs_snapshot()
     samurai = self.samurai("-C", self.tmpdir, "-p",
                            self.MANIFEST_FILENAME, self.BUILD_DIR)
     self.assertProcReturn(samurai, 0)
     self.pdbg("====== build.ninja")
     self.pdbg(read_file(self.build_ninja_file()))
     self.pdbg("====== sub/build.ninja")
     self.pdbg(read_file(sub_build_ninja_file))
     self.assertFSDiff({self.build_ninja_file():FSDiffOp.CREATED,
                        self.decl_ninja_file():FSDiffOp.CREATED,
                        sub_build_ninja_file:FSDiffOp.CREATED,
                        sub_decl_ninja_file:FSDiffOp.CREATED},
                       "all ninja files not generated")
     ### 2nd run: build outputs
     self.pdbg("====== #2")
     ninja = self.ninja("-C", self.build_dir())
     self.assertProcReturn(ninja, 0)
     self.assertFSDiff({output:FSDiffOp.CREATED,
                        sub_output:FSDiffOp.CREATED},
                       "output files not built")
     self.assertEqual(read_file(output), content+"\n")
     self.assertEqual(read_file(sub_output), sub_content+"\n")
     ### 3rd run: nothing to do
     self.pdbg("====== #3")
     ninja = self.ninja("-C", self.build_dir())
     self.assertEqual(ninja.returncode, 0)
     self.assertFSDiff({}, "nothing has changed")
     ### 4th run: touched sub-manifest makes ninja regenerates only the
     ###          sub-project.
     self.pdbg("====== #4")
     time.sleep(1)
     touch(fs["sub"][self.MANIFEST_FILENAME])
     self.assertFSDiff({fs["sub"][self.MANIFEST_FILENAME]:FSDiffOp.UPDATED},
                       "sub samurai manifest not touched")
     ninja = self.ninja("-C", self.build_dir())
     self.assertProcReturn(ninja, 0)
     self.assertFSDiff({sub_build_ninja_file:FSDiffOp.UPDATED,
                        sub_decl_ninja_file:FSDiffOp.UPDATED},
                       "sub ninja files not re-generated")
     ### 5th run: touched manifest makes ninja regenerates only its
     ###          manifest but not the one of the sub-projects.
     self.pdbg("====== #5")
     time.sleep(1)
     touch(fs[self.MANIFEST_FILENAME])
     self.assertFSDiff({fs[self.MANIFEST_FILENAME]:FSDiffOp.UPDATED},
                       "samurai manifest not touched")
     ninja = self.ninja("-C", self.build_dir())
     self.assertProcReturn(ninja, 0)
     self.assertFSDiff({self.build_ninja_file():FSDiffOp.UPDATED,
                        self.decl_ninja_file():FSDiffOp.UPDATED},
                       "ninja files not re-generated")
     ### 6th run: touched both manifest makes ninja regenerates both
     self.pdbg("====== #6")
     time.sleep(1)
     touch(fs[self.MANIFEST_FILENAME])
     touch(fs["sub"][self.MANIFEST_FILENAME])
     self.assertFSDiff({fs[self.MANIFEST_FILENAME]:FSDiffOp.UPDATED,
                        fs["sub"][self.MANIFEST_FILENAME]:FSDiffOp.UPDATED},
                       "all samurai manifest not touched")
     ninja = self.ninja("-C", self.build_dir())
     self.assertProcReturn(ninja, 0)
     self.assertFSDiff({self.build_ninja_file():FSDiffOp.UPDATED,
                        self.decl_ninja_file():FSDiffOp.UPDATED,
                        sub_build_ninja_file:FSDiffOp.UPDATED,
                        sub_decl_ninja_file:FSDiffOp.UPDATED},
                       "all ninja files not re-generated")
Ejemplo n.º 6
0
 def test_accept_pathlib(self):
     p = PathLib(os.path.join(self.tmpdir, "x"))
     touch(p)
     self.assertTrue(p.exists())
Ejemplo n.º 7
0
 def test_touch_raises_error_if_no_path(self):
     with self.assertRaises(FileNotFoundError):
         touch(os.path.join(self.tmpdir, "doesnotexists", "afile"))
Ejemplo n.º 8
0
 def test_touch_make_dirs(self):
     subdir = os.path.join(self.tmpdir, "doesnotexists")
     path = os.path.join(subdir, "afile")
     self.assertFalse(os.path.isfile(path))
     touch(path, make_dirs=True)
     self.assertTrue(os.path.isfile(path))
Ejemplo n.º 9
0
 def test_file_exists(self):
     p = os.path.join(self.tmpdir, "x")
     touch(p)
     self.assertTrue(os.path.exists(p))
     rm_f(p)
     self.assertFalse(os.path.exists(p))