Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty string replacement (--replace "") is interpreted as absence of parameter #117

Closed
kdeldycke opened this issue Jan 5, 2024 · 3 comments · Fixed by #119
Closed

Empty string replacement (--replace "") is interpreted as absence of parameter #117

kdeldycke opened this issue Jan 5, 2024 · 3 comments · Fixed by #119
Labels
bug Something isn't working

Comments

@kdeldycke
Copy link
Collaborator

  • bump-my-version version: 0.15.4
  • Python version: 3.12.1
  • Operating System: macOS 14.2.1

Description

A simple search and replace of plain string by an empty one is polluted by non-matching rules from the configuration file.

This is a follow-up of #70.

What I Did

Here are my files:

  • pyproject.toml:

    [tool.bumpversion]
    current_version = "2.21.1"
    allow_dirty = true
    
    [[tool.bumpversion.files]]
    filename = "./changelog.md"
    search = "random"
    replace = "blah_blah_blah"
  • changelog.md:

     Changelog
    
    ## [2.21.1 (unreleased)](https://github.com/kdeldycke/workflows/compare/v2.21.0...main)
    
    - Test profanity.

Here I'm trying to replace the profanity string by XXX with this CLI invokation, and it works:

$ bump-my-version replace --dry-run --verbose --no-configured-files --allow-dirty --ignore-missing-version --search "profanity" --replace "XXX" ./changelog.md
Starting BumpVersion 0.15.4                                                                                                                                                                                         
Reading configuration                                                                                                                                                                                               
  Reading config file: /Users/kde/bump_test/pyproject.toml                                                                                                                                                          
Parsing version '2.21.1' using regexp '(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)'                                                                                                                              
  Parsed the following values: major=2, minor=21, patch=1                                                                                                                                                           
                                                                                                                                                                                                                    
File ./changelog.md: replace `profanity` with `XXX`                                                                                                                                                                 
  Found 'profanity' at line 5: profanity                                                                                                                                                                            
  Would change file ./changelog.md:                                                                                                                                                                                 
    *** before ./changelog.md                                                                                                                                                                                       
    --- after ./changelog.md                                                                                                                                                                                        
    ***************                                                                                                                                                                                                 
    *** 2,5 ****                                                                                                                                                                                                    
                                                                                                                                                                                                                    
      ## [2.21.1 (unreleased)](https://github.com/kdeldycke/workflows/compare/v2.21.0...main)                                                                                                                       
                                                                                                                                                                                                                    
    ! - Test profanity.                                                                                                                                                                                             
    --- 2,5 ----                                                                                                                                                                                                    
                                                                                                                                                                                                                    
      ## [2.21.1 (unreleased)](https://github.com/kdeldycke/workflows/compare/v2.21.0...main)                                                                                                                       
                                                                                                                                                                                                                    
    ! - Test XXX.                                                                                                                                                                                                   
                                                                                                                                                                                                                    
File ./changelog.md: replace `profanity` with `XXX`                                                                                                                                                                 
  Found 'profanity' at line 5: profanity                                                                                                                                                                            
  Would change file ./changelog.md:                                                                                                                                                                                 
    *** before ./changelog.md                                                                                                                                                                                       
    --- after ./changelog.md                                                                                                                                                                                        
    ***************                                                                                                                                                                                                 
    *** 2,5 ****                                                                                                                                                                                                    
                                                                                                                                                                                                                    
      ## [2.21.1 (unreleased)](https://github.com/kdeldycke/workflows/compare/v2.21.0...main)                                                                                                                       
                                                                                                                                                                                                                    
    ! - Test profanity.                                                                                                                                                                                             
    --- 2,5 ----                                                                                                                                                                                                    
                                                                                                                                                                                                                    
      ## [2.21.1 (unreleased)](https://github.com/kdeldycke/workflows/compare/v2.21.0...main)                                                                                                                       
                                                                                                                                                                                                                    
    ! - Test XXX.    

It works as expected, with the only small annoyance that the replacement action seems to be called twice. Maybe this is just a detail of the way the replacement action is reported, or it might indicate a trickier problem. Anyway.

Now instead of XXX, I want to remove the profanity entirely, and replace it by an empty string.

So here I call the same CLI as above, but use the --replace "" option instead:

$ bump-my-version replace --dry-run --verbose --no-configured-files --allow-dirty --ignore-missing-version --search "profanity" --replace "" ./changelog.md
Starting BumpVersion 0.15.4                                                                                                                                                                                         
Reading configuration                                                                                                                                                                                               
  Reading config file: /Users/kde/bump_test/pyproject.toml                                                                                                                                                          
Parsing version '2.21.1' using regexp '(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)'                                                                                                                              
  Parsed the following values: major=2, minor=21, patch=1                                                                                                                                                           
                                                                                                                                                                                                                    
File ./changelog.md: replace `profanity` with `{new_version}`                                                                                                                                                       
  Found 'profanity' at line 5: profanity                                                                                                                                                                            
  Would change file ./changelog.md:                                                                                                                                                                                 
    *** before ./changelog.md                                                                                                                                                                                       
    --- after ./changelog.md                                                                                                                                                                                        
    ***************                                                                                                                                                                                                 
    *** 2,5 ****                                                                                                                                                                                                    
                                                                                                                                                                                                                    
      ## [2.21.1 (unreleased)](https://github.com/kdeldycke/workflows/compare/v2.21.0...main)                                                                                                                       
                                                                                                                                                                                                                    
    ! - Test profanity.                                                                                                                                                                                             
    --- 2,5 ----                                                                                                                                                                                                    
                                                                                                                                                                                                                    
      ## [2.21.1 (unreleased)](https://github.com/kdeldycke/workflows/compare/v2.21.0...main)                                                                                                                       
                                                                                                                                                                                                                    
    ! - Test 2.21.1.                                                                                                                                                                                                
                                                                                                                                                                                                                    
File ./changelog.md: replace `profanity` with `blah_blah_blah`                                                                                                                                                      
  Found 'profanity' at line 5: profanity                                                                                                                                                                            
  Would change file ./changelog.md:                                                                                                                                                                                 
    *** before ./changelog.md                                                                                                                                                                                       
    --- after ./changelog.md                                                                                                                                                                                        
    ***************                                                                                                                                                                                                 
    *** 2,5 ****                                                                                                                                                                                                    
                                                                                                                                                                                                                    
      ## [2.21.1 (unreleased)](https://github.com/kdeldycke/workflows/compare/v2.21.0...main)                                                                                                                       
                                                                                                                                                                                                                    
    ! - Test profanity.                                                                                                                                                                                             
    --- 2,5 ----                                                                                                                                                                                                    
                                                                                                                                                                                                                    
      ## [2.21.1 (unreleased)](https://github.com/kdeldycke/workflows/compare/v2.21.0...main)                                                                                                                       
                                                                                                                                                                                                                    
    ! - Test blah_blah_blah.                                                                                                                                                                                        

I expect there the changed line in ./changelog.md to be - Test ., but instead, I get two conflicting results:

  • the first in which profanity is replaced by the current version (2.21.1)
  • the second in which profanity is replaced by the unrelated blah_blah_blah string that has been hard-coded in the pyproject.toml file

By the nature of this edge-case, it feels like somewhere in the code path, the empty string passed to --replace is normalized to None, as if --replace hasn't been used at all.

@kdeldycke kdeldycke added the bug Something isn't working label Jan 5, 2024
@kdeldycke
Copy link
Collaborator Author

By the nature of this edge-case, it feels like somewhere in the code path, the empty string passed to --replace is normalized to None, as if --replace hasn't been used at all.

That's indeed the case: --replace "" is interpreted as the absence of --replace option altogether.

The same CLI as above, without the --replace "" option produce the same results:

$ bump-my-version replace --dry-run --verbose --no-configured-files --allow-dirty --ignore-missing-version --search "profanity" ./changelog.md
Starting BumpVersion 0.15.4                                                                                                                                                                                         
Reading configuration                                                                                                                                                                                               
  Reading config file: /Users/kde/bump_test/pyproject.toml                                                                                                                                                          
Parsing version '2.21.1' using regexp '(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)'                                                                                                                              
  Parsed the following values: major=2, minor=21, patch=1                                                                                                                                                           
                                                                                                                                                                                                                    
File ./changelog.md: replace `profanity` with `{new_version}`                                                                                                                                                       
  Found 'profanity' at line 5: profanity                                                                                                                                                                            
  Would change file ./changelog.md:                                                                                                                                                                                 
    *** before ./changelog.md                                                                                                                                                                                       
    --- after ./changelog.md                                                                                                                                                                                        
    ***************                                                                                                                                                                                                 
    *** 2,5 ****                                                                                                                                                                                                    
                                                                                                                                                                                                                    
      ## [2.21.1 (unreleased)](https://github.com/kdeldycke/workflows/compare/v2.21.0...main)                                                                                                                       
                                                                                                                                                                                                                    
    ! - Test profanity.                                                                                                                                                                                             
    --- 2,5 ----                                                                                                                                                                                                    
                                                                                                                                                                                                                    
      ## [2.21.1 (unreleased)](https://github.com/kdeldycke/workflows/compare/v2.21.0...main)                                                                                                                       
                                                                                                                                                                                                                    
    ! - Test 2.21.1.                                                                                                                                                                                                
                                                                                                                                                                                                                    
File ./changelog.md: replace `profanity` with `blah_blah_blah`                                                                                                                                                      
  Found 'profanity' at line 5: profanity                                                                                                                                                                            
  Would change file ./changelog.md:                                                                                                                                                                                 
    *** before ./changelog.md                                                                                                                                                                                       
    --- after ./changelog.md                                                                                                                                                                                        
    ***************                                                                                                                                                                                                 
    *** 2,5 ****                                                                                                                                                                                                    
                                                                                                                                                                                                                    
      ## [2.21.1 (unreleased)](https://github.com/kdeldycke/workflows/compare/v2.21.0...main)                                                                                                                       
                                                                                                                                                                                                                    
    ! - Test profanity.                                                                                                                                                                                             
    --- 2,5 ----                                                                                                                                                                                                    
                                                                                                                                                                                                                    
      ## [2.21.1 (unreleased)](https://github.com/kdeldycke/workflows/compare/v2.21.0...main)                                                                                                                       
                                                                                                                                                                                                                    
    ! - Test blah_blah_blah.                                   
                                                                                                                                                     

@kdeldycke kdeldycke changed the title --replace with empty string is polluted by non-matching rule Empty string replacement (--replace "") is considered as absence of --replace parameter Jan 5, 2024
@kdeldycke kdeldycke changed the title Empty string replacement (--replace "") is considered as absence of --replace parameter Empty string replacement (--replace "") is considered as absence of parameter Jan 5, 2024
@kdeldycke kdeldycke changed the title Empty string replacement (--replace "") is considered as absence of parameter Empty string replacement (--replace "") is interpreted as absence of parameter Jan 5, 2024
@coordt
Copy link
Member

coordt commented Jan 5, 2024

Thanks for noticing this. I should be able to tackle this tomorrow.

coordt added a commit that referenced this issue Jan 6, 2024
Only a missing replacement value will trigger one of the fallback options.

Fixes #117
@coordt coordt linked a pull request Jan 6, 2024 that will close this issue
@kdeldycke
Copy link
Collaborator Author

Thanks @coordt for the fix! I can confirm v0.16.1 fix this issue! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants