diff --git a/CHANGELOG.md b/CHANGELOG.md index 00cbbd1..786fa60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## Pending Changes +### Added +- Ability to include pending changes in changelog for unreleased changes +- Test for the release preview flag + +--- ## 0.8.0 ### Added - Preview option for changelogs on the `release` and `bump` command diff --git a/README.md b/README.md index 5c4cefa..07c3fea 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,16 @@ $ codelog release 1.0.0 --preview ``` Will display a preview of your changes on your console as if the version **1.0.0** has been released. +### Add Pending Releases to Changelog + +You may find yourself wanting to include all of your unreleased changes in your changelog, whether it be for testing purposes or as a list of changes coming soon. The `pending` command does that for you. + +```bash +$ codelog pending +``` + +`TITLE` is an option argument that is effectively the "Version" pending. The default value is `Pending Changes`. + ## Configuring Since version 0.3.0, there are a few configurations that are possible. You can choose: diff --git a/changelogs/codelog.yml b/changelogs/codelog.yml index 452d18d..2361b04 100644 --- a/changelogs/codelog.yml +++ b/changelogs/codelog.yml @@ -2,6 +2,7 @@ header_textfile: changelogs/header.txt default_changelog_filename: CHANGELOG.md +default_pending_changes_title: "Pending Changes" # Tag formatting --------------------------------------------------- diff --git a/changelogs/releases/pending.md b/changelogs/releases/pending.md new file mode 100644 index 0000000..e4123a9 --- /dev/null +++ b/changelogs/releases/pending.md @@ -0,0 +1,6 @@ +## Pending Changes +### Added +- Ability to include pending changes in changelog for unreleased changes +- Test for the release preview flag + +--- diff --git a/changelogs/unreleased/20210407134249312_change.yml b/changelogs/unreleased/20210407134249312_change.yml new file mode 100644 index 0000000..090a7d1 --- /dev/null +++ b/changelogs/unreleased/20210407134249312_change.yml @@ -0,0 +1,3 @@ +"Added": + - Ability to include pending changes in changelog for unreleased changes + - Test for the release preview flag diff --git a/lib/codelog.rb b/lib/codelog.rb index 80cadac..0514b57 100644 --- a/lib/codelog.rb +++ b/lib/codelog.rb @@ -9,6 +9,7 @@ require 'codelog/command/regenerate' require 'codelog/command/release' require 'codelog/command/preview' +require 'codelog/command/pending' require 'codelog/command/bump' require 'codelog/command/step/changelog' require 'codelog/command/step/delete' diff --git a/lib/codelog/cli.rb b/lib/codelog/cli.rb index a2f45ca..a442200 100644 --- a/lib/codelog/cli.rb +++ b/lib/codelog/cli.rb @@ -37,6 +37,13 @@ def release(version_number, end end + desc 'pending <TITLE>', 'Generate pending changelog from unreleased files' + method_option :title, desc: 'Title to use for the unreleased changes section', + aliases: ['-t', '--title'], type: :string + def pending(title = Codelog::Config.pending_changes_title) + Codelog::Command::Pending.run title + end + desc 'bump [VERSION_TYPE] <RELEASE_DATE>', "Bumps the next version, \ being it major, minor or patch" method_option :preview, desc: 'Prints the preview of the next version', diff --git a/lib/codelog/command/pending.rb b/lib/codelog/command/pending.rb new file mode 100644 index 0000000..bb0dab6 --- /dev/null +++ b/lib/codelog/command/pending.rb @@ -0,0 +1,20 @@ +module Codelog + module Command + class Pending + RELEASES_PATH = 'changelogs/releases'.freeze + + def self.run(title) + Codelog::Command::Pending.new.run title + end + + def run(title) + outputter = Codelog::Output::ReleaseFile.new("#{RELEASES_PATH}/pending.md") + Codelog::Command::Step::Version.run title, + Date.today.strftime(Codelog::Config.date_input_format), + outputter + Codelog::Command::Step::Changelog.run + puts "\n== Pending changes added to changelog ==" + end + end + end +end diff --git a/lib/codelog/command/regenerate.rb b/lib/codelog/command/regenerate.rb index 815837b..f9d75fe 100644 --- a/lib/codelog/command/regenerate.rb +++ b/lib/codelog/command/regenerate.rb @@ -7,7 +7,7 @@ def self.run def run Codelog::Command::Step::Changelog.run - puts "The CHANGELOG was regenerated successfully!" + puts 'The CHANGELOG was regenerated successfully!' end end end diff --git a/lib/codelog/command/step/changelog.rb b/lib/codelog/command/step/changelog.rb index 4e11c0d..0c1c922 100644 --- a/lib/codelog/command/step/changelog.rb +++ b/lib/codelog/command/step/changelog.rb @@ -22,7 +22,7 @@ def changes version_changelogs = Dir['changelogs/releases/*.md'] version_changelogs.sort_by! do |file_name| version_number = file_name.split('/').last.chomp('.md') - Gem::Version.new(version_number) + version_number == 'pending' ? version_number : Gem::Version.new(version_number).to_s end.reverse! version_changelogs.inject([]) do |partial_changes, version_changelog| partial_changes + File.readlines(version_changelog) diff --git a/lib/codelog/command/step/delete.rb b/lib/codelog/command/step/delete.rb index f7fb7b4..c29e113 100644 --- a/lib/codelog/command/step/delete.rb +++ b/lib/codelog/command/step/delete.rb @@ -13,6 +13,7 @@ def self.run def run chdir Dir.pwd do system('rm -rv changelogs/unreleased/*.yml') + system('[ -f changelogs/releases/pending.md ] && rm -rv changelogs/releases/pending.md') end end end diff --git a/lib/codelog/config.rb b/lib/codelog/config.rb index d5c8190..ec0f3db 100644 --- a/lib/codelog/config.rb +++ b/lib/codelog/config.rb @@ -9,6 +9,10 @@ def filename settings['default_changelog_filename'] || 'CHANGELOG.md' end + def pending_changes_title + settings['default_pending_changes_title'] || 'Pending Changes' + end + def header File.open(settings['header_textfile'], 'r').read || '' end diff --git a/lib/codelog/output/release_file.rb b/lib/codelog/output/release_file.rb index f70bdd1..88260c0 100644 --- a/lib/codelog/output/release_file.rb +++ b/lib/codelog/output/release_file.rb @@ -11,7 +11,7 @@ def self.print(content, file_path) def print(content) Dir.chdir Dir.pwd do - File.open(@file_path, 'a') do |line| + File.open(@file_path, 'w+') do |line| line.puts content end end diff --git a/spec/codelog/cli_spec.rb b/spec/codelog/cli_spec.rb index 452e934..acb43ff 100644 --- a/spec/codelog/cli_spec.rb +++ b/spec/codelog/cli_spec.rb @@ -43,6 +43,31 @@ subject.release '1.2.3', '2012-12-12' expect(Codelog::Command::Release).to have_received(:run).with '1.2.3', '2012-12-12' end + + it 'calls the release command with the preview flag' do + allow(Codelog::Command::Preview).to receive(:run) + subject.options = {preview: true} + subject.release '1.2.3', '2012-12-12' + expect(Codelog::Command::Preview).to have_received(:run).with '1.2.3', '2012-12-12' + end + end + end + + describe '#regenerate' do + it 'calls the regenerate command' do + allow(Codelog::Command::Regenerate).to receive(:run) + subject.regenerate + expect(Codelog::Command::Regenerate).to have_received(:run) + end + end + + describe '#pending' do + context 'passing the title as an argument' do + it 'calls the pending command' do + allow(Codelog::Command::Pending).to receive(:run) + subject.pending 'Pending Changes' + expect(Codelog::Command::Pending).to have_received(:run).with 'Pending Changes' + end end end diff --git a/spec/codelog/command/pending_spec.rb b/spec/codelog/command/pending_spec.rb new file mode 100644 index 0000000..2dbc62b --- /dev/null +++ b/spec/codelog/command/pending_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper' + +describe Codelog::Command::Pending do + describe '#run' do + let(:mock_file_outputter) { double(Codelog::Output::ReleaseFile) } + + it 'adds the pending changes to the changelog' do + allow(Codelog::Command::Step::Version).to receive(:new) + allow(Codelog::Output::ReleaseFile).to receive(:new).and_return(mock_file_outputter) + + expect(Codelog::Command::Step::Version).to receive(:run).with('Pending Changes', Date.today.strftime(Codelog::Config.date_input_format), mock_file_outputter) + expect(Codelog::Command::Step::Changelog).to receive(:run) + expect(subject).to receive(:puts).with("\n== Pending changes added to changelog ==") + subject.run 'Pending Changes' + end + end + + describe '.run' do + it 'creates an instance of the class to run the command' do + expect_any_instance_of(described_class).to receive(:run).with 'Pending Changes' + described_class.run 'Pending Changes' + end + end +end diff --git a/spec/codelog/command/regenerate_spec.rb b/spec/codelog/command/regenerate_spec.rb index 2cfb611..ad8c526 100644 --- a/spec/codelog/command/regenerate_spec.rb +++ b/spec/codelog/command/regenerate_spec.rb @@ -1,12 +1,18 @@ require 'spec_helper' describe Codelog::Command::Regenerate do + describe '#run' do + it 'calls the "Changelog" step to re-generate the CHANGELOG using existing releases' do + expect(Codelog::Command::Step::Changelog).to receive(:run) + expect(subject).to receive(:puts).with("The CHANGELOG was regenerated successfully!") + subject.run + end + end + describe '.run' do it 'calls the "Changelog" step to re-generate the CHANGELOG using existing releases' do allow(Codelog::Command::Step::Changelog).to receive(:run) - described_class.run - expect(Codelog::Command::Step::Changelog).to have_received(:run) end end diff --git a/spec/codelog/command/step/changelog_spec.rb b/spec/codelog/command/step/changelog_spec.rb index d0b3882..3f77fed 100644 --- a/spec/codelog/command/step/changelog_spec.rb +++ b/spec/codelog/command/step/changelog_spec.rb @@ -21,6 +21,7 @@ it 'creates a changelog file from the releases' do allow(mocked_changelog).to receive(:puts) + expect(File).to receive(:open).with('changelogs/header.txt', 'r').and_return(mocked_header_textfile) expect(File).to receive(:open).with('CHANGELOG.md', 'w+').and_yield mocked_changelog subject.run expect(mocked_changelog).to have_received(:puts).with(['line_2\n', 'line_1\n']) diff --git a/spec/codelog/command/step/delete_spec.rb b/spec/codelog/command/step/delete_spec.rb index 3c2ffe0..25ac9a8 100644 --- a/spec/codelog/command/step/delete_spec.rb +++ b/spec/codelog/command/step/delete_spec.rb @@ -10,6 +10,8 @@ it 'removes all the files from the unreleased folder' do expect(subject).to have_received(:system) .with('rm -rv changelogs/unreleased/*.yml') + expect(subject).to have_received(:system) + .with('[ -f changelogs/releases/pending.md ] && rm -rv changelogs/releases/pending.md') end end diff --git a/spec/codelog/config_spec.rb b/spec/codelog/config_spec.rb index 3f5f11f..ac6d2ad 100644 --- a/spec/codelog/config_spec.rb +++ b/spec/codelog/config_spec.rb @@ -22,6 +22,12 @@ end end + describe '#pending_changes_title' do + it 'returns the correct pending changelog title' do + expect(described_class.pending_changes_title).to eq('Pending Changes') + end + end + describe '#header' do let(:mocked_header_file) { double(File, read: 'stubbed header') } diff --git a/spec/codelog/output/release_file_spec.rb b/spec/codelog/output/release_file_spec.rb index 9a27f9b..e079ac5 100644 --- a/spec/codelog/output/release_file_spec.rb +++ b/spec/codelog/output/release_file_spec.rb @@ -15,7 +15,7 @@ it 'prints the passed content in a file on the passed path' do subject - expect(File).to have_received(:open).with(file_path, 'a') + expect(File).to have_received(:open).with(file_path, 'w+') expect(mocked_file).to have_received(:puts).with(file_content) end end