GitLab CI/CD使用经验,来自于莫纳什大学的考试任务解析-CSDN博客

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

CI/CD简介

CI/CD的作用在于自动化和加速软件开发、测试和交付流程通过持续集成确保代码协同工作和质量通过持续交付降低风险使每次代码变更都能够快速、高质量地交付到生产环境从而提高软件开发效率、质量和协作。

作业要求

学校给出一个售货系统的代码需要学生对于代码进行构建CI/CDflake8mypypycodestylepydocstylepylint并且使用Python完善软件测试test。

解析方案

在构建CI/CD管道时在使用配置文件创建静态分析时我最初没有区分地将所有阶段合并在一起。在后来的过程中我通过为每个阶段使用相同的阶段名称来解决这个问题。当测试没有被代码纠正时发生的测试用例。
Below is my code:

stages:
  - Static Analysis 
  - Test

flake8:
  stage: Static Analysis
  script:
    - pip install flake8
    - flake8 megamart.py
  allow_failure: true

mypy:
  stage: Static Analysis  
  script:
    - pip install mypy
    - mypy megamart.py
  allow_failure: true

pycodestyle:
  stage: Static Analysis
  script: 
    - pip install pycodestyle
    - pycodestyle megamart.py 
  allow_failure: true

pydocstyle:
  stage: Static Analysis
  script:
    - pip install pydocstyle
    - pydocstyle megamart.py
  allow_failure: true

pylint:
  stage: Static Analysis
  script:
    - pip install pylint
    - pylint megamart.py
  allow_failure: true

testing:
  stage: Test
  script:
    - python -m unittest test_megamart.py
  allow_failure: false

在这里插入图片描述在修改完测试用例后可以登录你的GitLab可以看到你的测试结果已经通过但是你的代码风格分析产生一些错误异常

在这里插入图片描述

分析代码风格产生异常的原因

PyCodeStyles

使用Python绘制一个柱状图来查看代码风格产生异常的错误代码
在这里插入图片描述
接下来根据每一个错误给出解决错误的方式
D200: One-line docstring should fit on one line with quotes (found 4):
这个错误只需要在导入一个模块的时候加上备注写入对于模块的描述就可以解决这个问题下面是对比
An Example of the Original Version.

from datetime import datetime

An Example of the Fixed Version.

"""
This is the Megamart module.

It is Megamart
"""
from datetime import datetime

D205: 1 blank line required between summary line and description
An Example of the Original Version.

def is_not_allowed_to_purchase_item(item: Item, customer: Customer, purchase_date_string: str) -> bool:
  """
  Returns True if the customer is not allowed to purchase the specified item, False otherwise.
  If an item object or the purchase date string was not actually provided, an Exception should be raised.
  Items that are under the alcohol, tobacco or knives category may only be sold to customers who are aged 18+ and have their ID verified.
  An item potentially belongs to many categories - as long as it belongs to at least one of the three categories above, restrictions apply to that item.
  The checking of an item's categories against restricted categories should be done in a case-insensitive manner.
   For example, if an item A is in the category ['Alcohol'] and item B is in the category ['ALCOHOL'], both items A and B should be identified as restricted items.
  Even if the customer is aged 18+ and is verified, they must provide/link their member account to the transaction when purchasing restricted items.
  Otherwise, if a member account is not provided, they will not be allowed to purchase the restricted item even if normally allowed to.
  It is optional for customers to provide their date of birth in their profile.
  Purchase date string should be of the format dd/mm/yyyy.
  The age of the customer is calculated from their specified date of birth, which is also of the format dd/mm/yyyy.
  If an item is a restricted item but the purchase or birth date is in the incorrect format, an Exception should be raised.
  A customer whose date of birth is 01/08/2005 is only considered to be age 18+ on or after 01/08/2023.
  """

An Example of the Fixed Version.

def is_not_allowed_to_purchase_item(item: Item, customer: Customer, purchase_date_string: str) -> bool:
  """
  Returns True if the customer is not allowed to purchase the specified item, False otherwise.

  Args:
      item (Item): The item being purchased.
      customer (Customer): The customer attempting the purchase.
      purchase_date_string (str): The purchase date in the format dd/mm/yyyy.

  Raises:
      Exception: If the provided item is None.

  If an item object or the purchase date string was not actually provided, an Exception should be raised.
  Items that are under the alcohol, tobacco or knives category may only be sold to customers who are aged 18+ and have their ID verified.
  An item potentially belongs to many categories - as long as it belongs to at least one of the three categories above, restrictions apply to that item.
  The checking of an item's categories against restricted categories should be done in a case-insensitive manner.
  For example, if an item A is in the category ['Alcohol'] and item B is in the category ['ALCOHOL'], both items A and B should be identified as restricted items.
  Even if the customer is aged 18+ and is verified, they must provide/link their member account to the transaction when purchasing restricted items.
  Otherwise, if a member account is not provided, they will not be allowed to purchase the restricted item even if normally allowed to.
  It is optional for customers to provide their date of birth in their profile.
  Purchase date string should be of the format dd/mm/yyyy.
  The age of the customer is calculated from their specified date of birth, which is also of the format dd/mm/yyyy.
  If an item is a restricted item but the purchase or birth date is in the incorrect format, an Exception should be raised.
  A customer whose date of birth is 01/08/2005 is only considered to be age 18+ on or after 01/08/2023.
  """

pyLine

在这里插入图片描述

C0301: Line too long (103/100) (line-too-long)
An Example of the Original Version.

def is_not_allowed_to_purchase_item(item: Item, customer: Customer, purchase_date_string: str) -> bool:
  """
  Determine if the customer is allowed to purchase the specified item.

  Returns True if the customer is not allowed to purchase the specified item, False otherwise.

def is_not_allowed_to_purchase_item(item: Item, 
                                    customer: Customer, 
                                    purchase_date_string: str) -> bool:
  """
  Determine if the customer is allowed to purchase the specified item.

  Returns True if the customer is not allowed to purchase the specified item, False otherwise.

  Args:
      item (Item): The item being purchased.
      customer (Customer): The customer attempting the purchase.
      purchase_date_string (str): The purchase date in the format dd/mm/yyyy.

PyDocstyle

在这里插入图片描述

Lizard

在这里插入图片描述

Mypy

在这里插入图片描述

Flake8

在这里插入图片描述

对比汇总表

在这里插入图片描述
其实这个项目中出现的错误有很多但是因为篇幅问题我没有办法一一把示例代码给出所以在此我制作出一个统计表格总结下来CI/CD可以检测代码风格进行统一管理方便于不同工程师之间进行协同时因为有统一的代码和备注风格所以减少协同成本。

结论和建议

在"megamart"文件中最常见的问题是"pyflakes "其次是"pydocstyles "。这些问题大多与代码注释有关。为了解决这些问题建议保持代码注释简洁并遵循一致的代码格式标准。如果您正在使用集成开发环境(IDE)请考虑为自动代码格式化配置插件。

最复杂的函数是“checkout”因为它涉及18个逻辑条件包括检查空值和特定数据类型。

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6