Ejemplo n.º 1
0
        public void CreateCompiledEntry2_CreatesEntry_WithProvidedEntries(ICollection <JournalEntryFile> entries)
        {
            var          fileSystem     = CreateVirtualJournal(2019, 2019);
            const string rootDirectory  = "J:\\Current";
            var          ioFactory      = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess  = A.Fake <ISystemProcess>();
            var          markdownFiles  = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal        = Journal.Open(ioFactory, markdownFiles, systemProcess);
            var          expectedTags   = entries.SelectMany(x => x.Tags).Distinct();
            var          expectedBodies = entries.Select(x => x.Body).Distinct();

            journal.CreateCompiledEntry(entries.Cast <IJournalEntry>().ToList(), false);

            var compiledDirectory = fileSystem.Path.Combine(rootDirectory, "Compiled");
            var file     = fileSystem.Directory.GetFiles(compiledDirectory).Single();
            var fileText = fileSystem.File.ReadAllText(file);

            foreach (var body in expectedBodies)
            {
                fileText.Should().Contain(body);
            }

            JournalFrontMatter.FromFilePath(fileSystem, file).Tags.Should().OnlyContain(t => expectedTags.Contains(t));

            A.CallTo(() => systemProcess.Start(A <string> ._)).MustHaveHappened();
        }
Ejemplo n.º 2
0
        private protected Journal OpenJournal()
        {
            var fileSystem    = new FileSystem();
            var ioFactory     = new JournalReaderWriterFactory(fileSystem, Location);
            var markdownFiles = new MarkdownFiles(fileSystem, Location);

            return(Journal.Open(ioFactory, markdownFiles, SystemProcess));
        }
Ejemplo n.º 3
0
        public PipedEntries()
        {
            var          fileSystem    = CreateVirtualJournal(2019, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);

            _journal = Journal.Open(ioFactory, markdownFiles, systemProcess);
        }
Ejemplo n.º 4
0
        public void RenameTagDryRun_ThrowsException_WhenTagDoesExist()
        {
            var          fileSystem    = CreateVirtualJournal(2017, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            Assert.Throws <InvalidOperationException>(() => journal.RenameTagDryRun("superman"));
        }
Ejemplo n.º 5
0
        public void OpenRandomEntry_ThrowsException_WhenNoEntriesAreFound()
        {
            var          fileSystem    = CreateEmptyJournal();
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            Assert.Throws <InvalidOperationException>(() => journal.OpenRandomEntry());
        }
        protected override void RunJournalCommand()
        {
            var fileSystem          = new FileSystem();
            var readerWriterFactory = new JournalReaderWriterFactory(fileSystem, Location);
            var markdownFiles       = new MarkdownFiles(fileSystem, Location);
            var journal             = Journal.Open(readerWriterFactory, markdownFiles, SystemProcess);

            var entries = journal.GetRecentEntries(Limit);

            WriteObject(entries, true);
        }
Ejemplo n.º 7
0
        public void CreateNewEntry_ThrowsException_IfEntryAlreadyExists()
        {
            var          fileSystem    = CreateVirtualJournal(2019, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            Assert.Throws <JournalEntryAlreadyExistsException>(() => journal.CreateNewEntry(new LocalDate(2019, 1, 1), null, null));
        }
Ejemplo n.º 8
0
        public void CreateCompiledEntry2_OverwritesEntry_WhenEntryExistsAndOverwriteIsTrue(ICollection <JournalEntryFile> entries)
        {
            var          fileSystem    = CreateVirtualJournal(2019, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            journal.CreateCompiledEntry(entries.Cast <IJournalEntry>().ToList(), false);
            journal.CreateCompiledEntry(entries.Cast <IJournalEntry>().ToList(), true);
        }
Ejemplo n.º 9
0
        public void CreateCompiledEntry2_ThrowsException_WhenEntryExistsAndOverwriteIsFalse(ICollection <JournalEntryFile> entries)
        {
            var          fileSystem    = CreateVirtualJournal(2019, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory, BodyWrapWidth);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            journal.CreateCompiledEntry(entries.Cast <IJournalEntry>().ToList(), false);
            Assert.Throws <JournalEntryAlreadyExistsException>(() => journal.CreateCompiledEntry(entries.Cast <IJournalEntry>().ToList(), false));
        }
Ejemplo n.º 10
0
        public void OpenRandomEntry_OpensTaggedEntry_WhenTagIsProvided()
        {
            var          fileSystem    = CreateVirtualJournal(2019, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            journal.OpenRandomEntry(new[] { "cat" }, null);
            A.CallTo(() => systemProcess.Start(A <string> ._)).MustHaveHappened();
        }
Ejemplo n.º 11
0
        public void OpenRandomEntry_SearchesEntireJournal_WhenNoTagsOrDateRangeUsed()
        {
            var          fileSystem    = CreateVirtualJournal(2019, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory, BodyWrapWidth);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            var entry = journal.GetRandomEntry(new string[] { }, TagOperator.Any, null);

            entry.Should().NotBeNull();
        }
Ejemplo n.º 12
0
        public void OpenRandomEntry_ThrowsException_WhenNoEntriesFound()
        {
            var          fileSystem    = new MockFileSystem();
            const string rootDirectory = "J:\\Current";

            fileSystem.AddDirectory(rootDirectory);
            var ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory, BodyWrapWidth);
            var systemProcess = A.Fake <ISystemProcess>();
            var markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            Assert.Throws <InvalidOperationException>(() => journal.GetRandomEntry(new[] { "fake" }, TagOperator.Any, null));
        }
Ejemplo n.º 13
0
        private void FromAll()
        {
            var fileSystem    = new FileSystem();
            var markdownFiles = new MarkdownFiles(fileSystem, Location);

            var sorted = SortDirection == "Descending" ?
                         markdownFiles.FindAll().OrderByDescending(FileNameToDate) :
                         markdownFiles.FindAll().OrderBy(FileNameToDate);

            var filtered = Limit > 0 ? sorted.Take(Limit).Select(PathToPSObject) : sorted.Select(PathToPSObject);

            WriteObject(filtered, true);
        }
Ejemplo n.º 14
0
        public void GetRecentEntries_ReturnsAllEntries_WhenLimitIsLessThanOne(int limit)
        {
            var          fileSystem    = CreateVirtualJournal(2019, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            var entries = journal.GetRecentEntries(limit);

            entries.Count().Should().Be(fileSystem.AllFiles.Count());
        }
Ejemplo n.º 15
0
        public void OpenRandomEntry_SearchesEntireJournal_WhenNoTagsOrDateRangeUsed()
        {
            var          fileSystem    = CreateVirtualJournal(2019, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            journal.OpenRandomEntry(new string[] { }, null);

            A.CallTo(() => systemProcess.Start(A <string> ._)).MustHaveHappened();
        }
Ejemplo n.º 16
0
        public void OpenRandomEntry_ThrowsException_WhenNoTaggedEntriesFound()
        {
            var          fileSystem    = CreateVirtualJournal(2020, 2023);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            Assert.Throws <InvalidOperationException>(() => journal.OpenRandomEntry(new List <string> {
                "fake"
            }, null));
        }
Ejemplo n.º 17
0
        public void CreateCompiledEntry1_OverwritesEntry_WhenEntryExistsAndOverwriteIsTrue()
        {
            var          fileSystem    = CreateVirtualJournal(2019, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            var dateRange = new DateRange("2019-2-12", "2019-3-1");

            journal.CreateCompiledEntry(dateRange, tags: null, allTagsRequired: false, overwrite: false);
            journal.CreateCompiledEntry(dateRange, tags: null, allTagsRequired: false, overwrite: true);
        }
Ejemplo n.º 18
0
        public void RenameTagDryRun_ReturnsListOfEffectedFiles_Always()
        {
            var          fileSystem    = CreateVirtualJournal(2017, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);
            var          index         = journal.CreateIndex <MetaJournalEntry>();

            var blahCount = index["blah"].Count;

            journal.RenameTagDryRun("blah").Should().HaveCount(blahCount);
        }
        public void Invalidate()
        {
            var fileSystem          = new FileSystem();
            var store               = new FileStore <UserSettings>(fileSystem);
            var settings            = store.Load();
            var readerWriterFactory = new JournalReaderWriterFactory(fileSystem, settings.DefaultJournalRoot, 120);
            var markDownFiles       = new MarkdownFiles(fileSystem, settings.DefaultJournalRoot);
            var systemProcess       = SystemProcessFactory.Create();
            var journal             = Journal.Open(readerWriterFactory, markDownFiles, systemProcess);
            var uniqueTags          = journal.GetUniqueTags();

            MemoryCache.Default.Set(nameof(JournalTagCache), uniqueTags, _policy);
            _tags = uniqueTags;
        }
Ejemplo n.º 20
0
        public void CreateCompiledEntry1_ThrowsException_WhenEntryExistsAndOverwriteIsFalse()
        {
            var          fileSystem    = CreateVirtualJournal(2019, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            var dateRange = new DateRange("2019-2-12", "2019-3-1");

            journal.CreateCompiledEntry(dateRange, null, false, false);
            Assert.Throws <JournalEntryAlreadyExistsException>(() => journal.CreateCompiledEntry(dateRange, null, false, false));
        }
Ejemplo n.º 21
0
        public void OpenRandomEntry_OpensEntryWithinDateRange_WhenRangeIsProvided()
        {
            var          fileSystem    = CreateVirtualJournal(2019, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory, BodyWrapWidth);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);
            var          entry         = journal.GetRandomEntry(null, TagOperator.Any, new DateRange(new LocalDate(2019, 6, 1), new LocalDate(2019, 6, 15)));

            var nameElements = entry.EntryName.Split(new[] { '.' });
            var intElements  = nameElements.Select(int.Parse).ToArray();

            Assert.True(intElements[0] == 2019 && intElements[1] == 6 && intElements[2] >= 1 && intElements[2] <= 15);
        }
Ejemplo n.º 22
0
        public void GetReadmeEntries_ExcludesFutureEntries_WhenSpecified()
        {
            var          earliestDate  = new LocalDate();
            var          fileSystem    = CreateVirtualJournal(2030, 2030);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);
            var          readmes       = journal.GetReadmeEntries(earliestDate, false);

            // Because the journal only includes entries from 2030, and all readmes have relative dates specified (5 years),
            // all readme entries should have a future expiration. Note that in 2035, this test will need to be updated. ;)
            readmes.Count.Should().Be(0);
        }
Ejemplo n.º 23
0
        public void GetReadmeEntries_FiltersOutEntriesOlderThanEarliestDate()
        {
            var          earliestDate  = new LocalDate(2009, 4, 25);
            var          fileSystem    = CreateVirtualJournal(2005, 2010);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);
            var          readmes       = journal.GetReadmeEntries(earliestDate, false);

            var count = readmes.Count(r => r.ReadmeDate <= earliestDate);

            count.Should().Be(0);
        }
Ejemplo n.º 24
0
        public void CreateCompiledEntry1_ThrowsException_WhenNoEntriesFoundInDateRange()
        {
            var          fileSystem    = CreateVirtualJournal(2019, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            var dateRange = new DateRange("2018-2-12", "2018-6-1");

            Assert.Throws <InvalidOperationException>(() =>
                                                      journal.CreateCompiledEntry(dateRange, tags: null, allTagsRequired: true, overwrite: false)
                                                      );
        }
Ejemplo n.º 25
0
        public void GetReadmeEntries_IncludesFutureEntries_WhenSpecified()
        {
            var          earliestDate  = new LocalDate();
            var          fileSystem    = CreateVirtualJournal(2030, 2031);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);
            var          readmes       = journal.GetReadmeEntries(earliestDate, true);

            // Since the earliest entries in this journal are dated 2030, and the min expiration date is 5 years,
            // none of them will have expired before 2035. Therefore, all readmes in this collection should be included.
            // This test will also have to be updated in 2035. F**k, I'll be 57 years old!
            readmes.Count.Should().Be(fileSystem.TotalReadmeEntries);
        }
Ejemplo n.º 26
0
        public void GetReadmeEntries_ReturnsAllEntries_WhenMaxDateIsDefaultValue()
        {
            var          earliestDate  = new LocalDate();
            var          fileSystem    = CreateVirtualJournal(2005, 2010);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);
            var          readmes       = journal.GetReadmeEntries(earliestDate, false);

            // Since this journal only has entries up to 2010, and the max expiration date is 5 years,
            // by "now" (e.g. the current date) they should have all expired. Therefore, the "include future" option
            // is effectively irrelevant in this scenario.
            readmes.Count.Should().Be(fileSystem.TotalReadmeEntries);
        }
Ejemplo n.º 27
0
        public void RenameTagDryRun_DoesNotInvokeJournalWriter_Ever()
        {
            var          fileSystem    = CreateVirtualJournal(2017, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = A.Fake <IJournalReaderWriterFactory>();
            var          writer        = A.Fake <IJournalWriter>();

            A.CallTo(() => ioFactory.CreateWriter()).Returns(writer);
            A.CallTo(() => ioFactory.CreateReader(A <string> .Ignored)).ReturnsLazily((string file) => new JournalReader(fileSystem, file));
            var systemProcess = A.Fake <ISystemProcess>();
            var markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            journal.RenameTagDryRun("blah");
            A.CallTo(() => writer.RenameTag(A <IJournalReader> ._, A <string> ._, A <string> ._)).MustNotHaveHappened();
        }
Ejemplo n.º 28
0
        public void CreateIndex_SkipsEntries_OutsideDateRange()
        {
            var          fileSystem    = CreateVirtualJournal(2017, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            var from      = new LocalDate(2019, 1, 1);
            var to        = new LocalDate(2019, 1, 10);
            var dateRange = new DateRange(from, to);
            var index     = journal.CreateIndex <JournalEntryFile>(dateRange);

            index.SelectMany(x => x.Entries).All(x => x.EntryDate >= from && x.EntryDate <= to).Should().BeTrue();
        }
Ejemplo n.º 29
0
        public void CreateCompiledEntry1_IncludesAllDates_WhenNoFiltersAreSpecified()
        {
            var fileSystem = CreateVirtualJournal(2016, 2019, onlyValidEntries: true);

            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            var dateRange = new DateRange("2016-1-1", "2019-12-28"); // Virtual journal assumes 28 days in a month.
            var filePath  = ioFactory.CreateWriter().GetCompiledJournalEntryFilePath(dateRange);

            journal.CreateCompiledEntry(range: null, tags: null, allTagsRequired: false, overwrite: false);

            fileSystem.FileExists(fileSystem.Path.Combine(rootDirectory, "Compiled", filePath)).Should().BeTrue();
        }
Ejemplo n.º 30
0
        public void CreateCompiledEntry2_ThrowsException_WhenNoEntriesProvided(ICollection <IJournalEntry> entries)
        {
            var          fileSystem    = CreateVirtualJournal(2019, 2019);
            const string rootDirectory = "J:\\Current";
            var          ioFactory     = new JournalReaderWriterFactory(fileSystem, rootDirectory);
            var          systemProcess = A.Fake <ISystemProcess>();
            var          markdownFiles = new MarkdownFiles(fileSystem, rootDirectory);
            var          journal       = Journal.Open(ioFactory, markdownFiles, systemProcess);

            Assert.Throws <ArgumentException>(() =>
                                              journal.CreateCompiledEntry(entries, false)
                                              );

            Assert.Throws <ArgumentException>(() =>
                                              journal.CreateCompiledEntry(null, false)
                                              );
        }