Ejemplo n.º 1
0
        public void GenerateXmlForDirectoryTest()
        {
            ManualResetEvent resetEvent = new ManualResetEvent(false);

            var           archive           = new SrcMLArchive(ArchiveDirectory, false, new SrcMLGenerator(Path.Combine(SrcMLHelper.GetSrcMLRootDirectory(), SrcMLHelper.srcMLExecutableLocation)));
            FileEventType expectedEventType = FileEventType.FileAdded;
            FileEventType actualEventType   = FileEventType.FileChanged;

            archive.FileChanged += (sender, e) => {
                actualEventType = e.EventType;
                bool shouldHaveSrcML = (e.EventType != FileEventType.FileDeleted);
                Assert.AreEqual(shouldHaveSrcML, e.HasSrcML);
                resetEvent.Set();
            };

            Dictionary <string, string> sourceFiles = new Dictionary <string, string>()
            {
                { Path.Combine(SourceDirectory, "foo.c"), String.Format(@"int foo() {{{0}printf(""hello world!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "bar.c"), String.Format(@"int bar() {{{0}    printf(""goodbye, world!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir1", "foo1.c"), String.Format(@"int foo1() {{{0}printf(""hello world 1!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir1", "bar1.c"), String.Format(@"int bar1() {{{0}    printf(""goodbye, world 1!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir2", "foo2.c"), String.Format(@"int foo2() {{{0}printf(""hello world 2!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir2", "bar2.c"), String.Format(@"int bar2() {{{0}    printf(""goodbye, world 2!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir1", "subdir11", "foo11.c"), String.Format(@"int foo11() {{{0}printf(""hello world 11!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir1", "subdir11", "bar11.c"), String.Format(@"int bar11() {{{0}    printf(""goodbye, world 11!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir1", "subdir12", "foo12.c"), String.Format(@"int foo12() {{{0}printf(""hello world 12!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir1", "subdir12", "bar12.c"), String.Format(@"int bar12() {{{0}    printf(""goodbye, world 12!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir2", "subdir21", "foo21.c"), String.Format(@"int foo21() {{{0}printf(""hello world 21!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir2", "subdir21", "bar21.c"), String.Format(@"int bar21() {{{0}    printf(""goodbye, world 21!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir2", "subdir22", "foo22.c"), String.Format(@"int foo22() {{{0}printf(""hello world 22!"");{0}}}", Environment.NewLine) },
                { Path.Combine(SourceDirectory, "subdir2", "subdir22", "bar22.c"), String.Format(@"int bar22() {{{0}    printf(""goodbye, world 22!"");{0}}}", Environment.NewLine) },
            };

            foreach (var fileDataPair in sourceFiles)
            {
                var directory = Path.GetDirectoryName(fileDataPair.Key);
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
                File.WriteAllText(fileDataPair.Key, fileDataPair.Value);
                archive.AddOrUpdateFile(fileDataPair.Key);
                Assert.That(resetEvent.WaitOne(300));
                Assert.AreEqual(expectedEventType, actualEventType);
            }

            foreach (var fileName in sourceFiles.Keys)
            {
                Assert.That(archive.ContainsFile(fileName), String.Format("Archive should contain {0}", fileName));
            }

            var changedFileName     = Path.Combine(SourceDirectory, "foo.c");
            var changedFileContents = String.Format(@"int foo() {{{0}printf(""hello world! changed"");{0}}}", Environment.NewLine);

            expectedEventType = FileEventType.FileChanged;
            File.WriteAllText(changedFileName, changedFileContents);
            File.SetLastWriteTime(changedFileName, DateTime.Now);

            Assert.That(archive.ContainsFile(changedFileName));
            Assert.That(archive.IsOutdated(changedFileName));

            archive.AddOrUpdateFile(changedFileName);
            Assert.That(resetEvent.WaitOne(300));
            Assert.AreEqual(expectedEventType, actualEventType);

            expectedEventType = FileEventType.FileDeleted;
            var deletedFileName = Path.Combine(SourceDirectory, "subdir1", "subdir12", "bar12.c");

            File.Delete(deletedFileName);
            Assert.That(archive.IsOutdated(deletedFileName));
            archive.DeleteFile(deletedFileName);
            Assert.That(resetEvent.WaitOne(300));
            Assert.AreEqual(expectedEventType, actualEventType);

            expectedEventType = FileEventType.FileRenamed;
            var movedFileName   = Path.Combine(SourceDirectory, "subdir1", "subdir11", "foo11.c");
            var newNameForMoved = Path.Combine(SourceDirectory, "subdir1", "subdir11", "foo1111111.c");

            File.Move(movedFileName, newNameForMoved);
            Assert.That(archive.IsOutdated(movedFileName));
            archive.RenameFile(movedFileName, newNameForMoved);
            Assert.That(resetEvent.WaitOne(300));
            Assert.AreEqual(expectedEventType, actualEventType);
            Assert.That(archive.ContainsFile(newNameForMoved));
            Assert.IsFalse(archive.ContainsFile(movedFileName));
        }